I am writing a program in C and it takes the name of an input file like data1 or
ID: 3800395 • 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>
#include <stdlib.h>
void readFile(char *fileName);
int main(int argc, char **argv)
{
if(argc<2){
printf("Please give file as input in command line ");
return 1;
}
char *file_name = argv[1];
printf("File name is : %s ",file_name);
readFile(file_name);
return 0;
}
void readFile(char *fileName)
{
FILE *file;
int totalV=0;
int count_a=0;
int count_e=0;
int count_i=0;
int count_o=0;
int count_u=0;
char ch;
file = fopen(fileName, "r");
do
{
ch = (char)fgetc(file);
if(ch=='a'|| ch=='A'){
count_a++;
totalV++;
}
if(ch=='e'|| ch=='E'){
count_e++;
totalV++;
}
if(ch=='i'|| ch=='I'){
count_i++;
totalV++;
}
if(ch=='o'|| ch=='O'){
count_o++;
totalV++;
}
if(ch=='u'|| ch=='U'){
count_u++;
totalV++;
}
} while(ch != EOF);
printf(" %d, %f percent, were a/A ",count_a,((float)count_a/(float)totalV)*100.0);
printf(" %d, %f percent, were E/E ",count_e,((float)count_e/(float)totalV)*100.0);
printf(" %d, %f percent, were i/I ",count_i,((float)count_i/(float)totalV)*100.0);
printf(" %d, %f percent, were o/O ",count_o,((float)count_o/(float)totalV)*100.0);
printf(" %d, %f percent, were u/U ",count_u,((float)count_u/(float)totalV)*100.0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.