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
Why c is called a middle level language?
What is the size of a union variable?
Write a program to find factorial of a number using recursive function.
what is different between auto and local static? why should we use local static?
write a program to concatenation the string using switch case?
What is the use of a semicolon (;) at the end of every program statement?
Explain about block scope in c?
the constant value in the case label is followed by a a) semicolon b) colon c) braces d) none of the above
Describe the order of precedence with regards to operators in C.
What is return type in c?
What is build process in c?
What is the difference between fread buffer() and fwrite buffer()?
What is hash table in c?
What are linked lists in c?
What is the difference between test design and test case design?