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

What are logical errors and how does it differ from syntax errors?

641


What does. int *x[](); means ?

624


What is a struct c#?

592


Mention four important string handling functions in c languages .

615


Why is struct padding needed?

619






Which header file is used for clrscr?

565


Explain argument and its types.

587


Write a C++ program to generate 10 integer numbers between - 1000 and 1000, then store the summation of the odd positive numbers in variable call it sum_pos, then find the maximum digit in this variable regardless of its digits length.

1556


what is the significance of static storage class specifier?

1650


Explain what standard functions are available to manipulate strings?

601


What are the different types of control structures in programming?

650


What is || operator and how does it function in a program?

618


What is the difference between local variable and global variable in c?

677


What is #include in c?

587


How do you determine the length of a string value that was stored in a variable?

642