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 is a c token and types of c tokens?

688


Explain the advantages of using macro in c language?

668


Explain what is the difference between a free-standing and a hosted environment?

744


which of the following is not a character constant a) 'thank you' b) 'enter values of p, n ,r' c) '23.56E-o3' d) all of the above

1622


Explain argument and its types.

701






what is the differnce between programing langauge and tool? is sas is a programing langauge r tool?

2002


explain what are pointers?

703


Explain what are global variables and explain how do you declare them?

739


c language supports bitwise operations, why a) 'c' language is system oriented b) 'c' language is problem oriented c) 'c' language is middle level language d) all the above

702


Once I have used freopen, how can I get the original stdout (or stdin) back?

723


What are identifiers in c?

751


In c programming write a program that will print 10 multiples of 3 except 15,18,21 using looping

1083


Are pointers integers in c?

700


program to convert a integer to string in c language'

2072


why use functions a) writing functions avoids rewriting the same code over and over b) using functions it becomes easier to write programs and keep track of what they are doing c) a & b d) none of the above

770