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

Answer Posted / mahfooz alam

#include <iostream>
#include<fstream>
using namespace std;
int main()
{
int arr[26]={0},i;
ifstream fin;
ofstream fout;
fout.open("input.txt",ios::app);
fin.open("input.txt",ios::out);
char c;
fin>>c;
while(!fin.eof())
{
if(isalpha(c))
{
tolower(c);
switch(c)
{
case 'a':
arr[0]++;
break;
case 'b':
arr[1]++;break;
case 'c':
arr[2]++;break;
case 'd':
arr[3]++;break;
case 'e':
arr[4]++;break;
case 'f':
arr[5]++;break;
case 'g':
arr[6]++;break;
case 'h':
arr[7]++;break;
case 'i':
arr[8]++;break;
case 'j':
arr[9]++;break;
case 'k':
arr[10]++;break;
case 'l':
arr[11]++;break;
case 'm':
arr[12]++;break;
case 'n':
arr[13]++;break;
case 'o':
arr[14]++;break;
case 'p':
arr[15]++;break;
case 'q':
arr[16]++;break;
case 'r':
arr[17]++;break;
case 's':
arr[18]++;break;
case 't':
arr[19]++;break;
case 'u':
arr[20]++;break;
case 'v':
arr[21]++;break;
case 'w':
arr[22]++;break;
case 'x':
arr[23]++;break;
case 'y':
arr[24]++;break;
case 'z':
arr[25]++;break;
}
}
fin>>c;
}//endl of while.*/
for(i=0;i<26;i++)
fout<<"no of letter "<<static_cast<char>(i+65)<<" is
"<<arr[i]<<endl;
fin.close();
return 0;
}

Is This Answer Correct ?    4 Yes 29 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain how can you restore a redirected standard stream?

807


What is a null pointer in c?

868


What is static volatile in c?

778


How #define works?

867


What is the difference between typedef struct and struct?

866


Explain what standard functions are available to manipulate strings?

843


shorting algorithmS

2045


Explain the use of function toupper() with and example code?

889


Write a C/C++ program to add a user to MySQL. The user should be permitted to only "INSERT" into the given database.

1741


simple program of graphics and their output display

1731


Write a program with dynamically allocation of variable.

883


Explain what is the difference between functions getch() and getche()?

813


Difference between linking and loading?

911


What is difference between static and global variable in c?

795


How do shell structures work?

815