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 is c so popular?
Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc...xyz in s2 . Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z. z-a:zyx......ba -1-6-:-123456- 1-9-1:123456789987654321 a-R-L:a-R...L a-b-c:abbc
Explain how can I manipulate strings of multibyte characters?
What is the maximum length of an identifier?
What is the purpose of macro in C language?
In c programming typeing to occupy the variables in memory space. if not useing the variable the memory space is wasted.ok, how to avoid the situation..? (the variable is used & notused)
Write a program that accept anumber in words
in multiple branching construct "default" case is a) optional b) compulsarily c) it is not include in this construct d) none of the above
code for replace tabs with equivalent number of blanks
What are the 5 data types?
Please send me WIPRO technical question to my mail ID.. its nisha_g28@yahoo.com please its urgent
What is wrong with this statement? Myname = 'robin';
Is using exit() the same as using return?
What are types of structure?
What are the c keywords?