I am writing a program in C and it takes the name of an input file like data1 or
ID: 3800397 • Letter: I
Question
I am writing a program in C and it takes the name of an input file like data1 or data 2 from the command line, then it counts the number of times each vowel occurs in the text (upper and lwer case) then it prints the percentages of the time the vowel was seen
percentage = (occurrances of vowel/total number of vowels)*100
The output is supposed to look like this:
CONTENTS OF data1
GIVE THEM HELL!
./a.out data1
#, % percent, were a/A
#, % percent, were e/E
# (of occurrances), % percent, were i/I
#, % percent, were o/O
#, % percent were u/U
Explanation / Answer
#include <stdio.h>
void main(int argc,char *argv[]) {
FILE *fp1;
int vowel=0,consonant=0;
char ch;
clrscr();
if(argc!=2) {
printf("Insufficient Arguments");
exit(0);
}
fp1=fopen(argv[1],"r");
if(fp1==NULL) {
printf("Source can't be opened");
exit(0);
}
ch=fgetc(fp1);
int vowelA = 0;
int vowelE = 0;
int vowelI = 0;
int vowelO =0;
int vowelU =0;
while(ch!=EOF) {
if((ch=='a')||(ch=='A')||(ch=='e')||(ch=='E')||(ch=='i')||(ch=='I')||(ch=='o') ||(ch=='O')||(ch=='u')||(ch=='U')){
vowel++;
if((ch=='a')||(ch=='A')){
vowelA++;
}
if((ch=='e')||(ch=='E')){
vowelE++;
}
if((ch=='i')||(ch=='I')){
vowelI++;
}
if((ch=='o') ||(ch=='O')){
vowelO++;
}
if((ch=='u')||(ch=='U')) {
vowelU++;
}
}
else
{
consonant++;
}
ch=fgetc(fp1);
}
printf(" %d, %3.2f were a/A ",vowelA,vowelA/vowel*100);
printf(" %d, %3.2f were e/E ",vowelA,vowelE/vowel*100);
printf(" %d, %3.2f were i/I ",vowelA,vowelI/vowel*100);
printf(" %d, %3.2f were o/O ",vowelA,vowelO/vowel*100);
printf(" %d, %3.2f were u/U ",vowelA,vowelU/vowel*100);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.