C program to find frequency of each character in a text
file?

Answer Posted / yogesh bansal

#include <stdio.h>

int main()
{
FILE *file;
int alpha[26]={0};
const char fileName[]="abc.txt";
char ch;
file= fopen(fileName,"r");
if(file == NULL)
{
printf("cannot open the %s",fileName);
exit(8);
}
do
{
ch=fgetc(file);
char c = tolower(ch);
switch(c)
{
case'a': alpha[0]++;
break;
case'b': alpha[1]++;
break;
case'c': alpha[2]++;
break;
case'd': alpha[3]++;
break;
case'e': alpha[4]++;
break;
case'f': alpha[5]++;
break;
case'g': alpha[6]++;
break;
case'h': alpha[7]++;
break;
case'i': alpha[8]++;
break;
case'j': alpha[9]++;
break;
case'k': alpha[10]++;
break;
case'l': alpha[11]++;
break;
case'm': alpha[12]++;
break;
case'n': alpha[13]++;
break;
case'o': alpha[14]++;
break;
case'p': alpha[15]++;
break;
case'q': alpha[16]++;
break;
case'r': alpha[17]++;
break;
case's': alpha[18]++;
break;
case't': alpha[19]++;
break;
case'u': alpha[20]++;
break;
case'v': alpha[21]++;
break;
case'w': alpha[22]++;
break;
case'x': alpha[23]++;
break;
case'y': alpha[24]++;
break;
case'z': alpha[25]++;
break;
}

}while(ch != EOF);
int i;
for(i=0;i<26;i++)
{
printf("%d\n",alpha[i]);
}
return 0;
}

this is the correct answer

Is This Answer Correct ?    23 Yes 20 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the benefits of c language?

653


Do pointers need to be initialized?

571


How to create struct variables?

597


Can you please explain the difference between exit() and _exit() function?

600


Explain a file operation in C with an example.

667






which of the following shows the correct hierarchy of arithmetic operations in C a) (), **, * or/,+ or - b) (),**,*,/,+,- c) (),**,/,*,+,- d) (),/ or *,- or +

1192


How can I handle floating-point exceptions gracefully?

640


a function gets called when the function name is followed by a a) semicolon (;) b) period(.) c) ! d) none of the above

883


write a program that declares an array of 30 elements named "income" in the main functions. then cal and pass the array to a programmer-defined function named "getIncome" within the "getIncome" function, ask the user for annual income of 30 employees. then calculate and print total income on the screen using the following function: "void getIncome ( ai []);

1854


Explain what is wrong with this statement? Myname = ?robin?;

1055


why programs in c are running with out #include? some warnings are display in terminal but we execute the program we get answer why? eg: main() { printf("hello world "); }

1323


writ a program to compare using strcmp VIVA and viva with its output.

1532


my project name is adulteration of chille powder.how can i explain it to the hr when he asks me about the project?

1130


What is the difference between if else and switchstatement

1320


what is ur strangth & weekness

1828