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

Answers were Sorted based on User's Feedback



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

Answer / vadivel

#include <stdio.h>
int count[26];
int main()
{
FILE *f;
int i;
char ch;
f = fopen("capti.c","r");
while(!feof(f))
{
ch = fgetc(f);
count[ch - 'a']++;
}
for(i = 0;i < 26;i++)
printf("count[%c] = %d\n",65+i,count[i]);
fclose(f);
return 0;
}
//asuming only lower characters are required

Is This Answer Correct ?    38 Yes 23 No

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

Answer / 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

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

Answer / dheeraj kumar

#include <stdio.h>

int Small_Alpha_Char[26];
int Capital_Alpha_Char[26];
int Numeric_Char[10];
int Special_Aplha_Char[100];

int main(){

char Name_Of_Source_File[100];
printf("Enter the Name of the Source File: ");
gets(Name_Of_Source_File);

FILE *ptr1;
ptr1 = fopen(Name_Of_Source_File, "r");

while(ptr1 == NULL){
printf("
We are facing issues to find the Source File!
");
printf("This file doesn't exist in your PC!
");

printf("Enter the Name of the Source File again: ");
gets(Name_Of_Source_File);

ptr1 = fopen(Name_Of_Source_File, "r");
}

char Each_Character;
int Count = 0;

while(!feof(ptr1)){

Each_Character = fgetc(ptr1);
if(Each_Character >= 'a' && Each_Character <= 'z'){
Small_Alpha_Char[Each_Character - 'a']++;
}
else if(Each_Character >= 'A' && Each_Character <= 'Z'){
Capital_Alpha_Char[Each_Character - 'A']++;
}
else if(Each_Character >= '0' && Each_Character <= '9'){
Numeric_Char[Each_Character - '0']++;
}
else{
Special_Aplha_Char[Each_Character - 0]++;
}
}

printf("List of Characters:
");
printf("Small Alphabetic Characters:
");
for(int i = 0; i < 26; i++){
if(Small_Alpha_Char[ i ] != 0){
printf("Count[%c] = %d
", 97 + i, Small_Alpha_Char[ i ]);
Count++;
}
}
printf("There are %d Small Characters which have Frequency greater than 0!

", Count);

Count = 0;
printf("Capital Alphabetic Characters:
");
for(int i = 0; i < 26; i++){
if(Capital_Alpha_Char[ i ] != 0){
printf("Count[%c] = %d
", 65 + i, Capital_Alpha_Char[ i ]);
Count++;
}
}
printf("There are %d Capital Characters which have Frequency greater than 0!

", Count);

Count = 0;
printf("Numeric Characters:
");
for(int i = 0; i < 9; i++){
if(Numeric_Char[ i ] != 0){
printf("Count[%c] = %d
", 48 + i, Numeric_Char[ i ]);
Count++;
}
}
printf("There are %d Numeric Characters which have Frequency greater than 0!

", Count);

Count = 0;
printf("Special Alphabetic Characters:
");
for(int i = 0; i < 99; i++){
if(Special_Aplha_Char[ i ] != 0){
printf("Count[%c] = %d
", i, Special_Aplha_Char[ i ]);
Count++;
}
}
printf("There are %d Special Characters which have Frequency greater than 0!
", Count);

fclose(ptr1);

return 0;
}

Is This Answer Correct ?    2 Yes 0 No

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

Answer / m. umar naeem

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ifstream ifile;
ifile.open("input.txt");
if (!ifile)
{
cout << "file not found!
";
return 1;
}
char a[26] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char c; int d[26] = { 0 };
while (ifile)
{
ifile.get(c);
if (c >= 'A' && c <= 'Z')
d[c - 65]++;
else if (c >= 'a' && c <= 'z')
d[c - 97]++;
}
for (int i = 0; i < 26; i++)
{
cout << a[i] << "=" << d[i] << endl;
}
ifile.close();
return 0;
}

Is This Answer Correct ?    0 Yes 1 No

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

Answer / silky

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

int main()
{
char a[100],s[100];
gets(a);
int i,c[27],j;

for(int i=0;i <=26;i++)
c[i]=0;

for(i=0;i<strlen(a);i++)
{
if(a[i]>='a' && a[i]<='z')
{
c[ a[i] - 'a' ]++;
}
}

for(j=0;j <=26;j++)
{
if(c[j]>0)
{
printf("%c occurs %d times\n ",'a'+j,c[j]);
}
}

}

Is This Answer Correct ?    5 Yes 8 No

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

Answer / 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

More C Interview Questions

Is main() is used in the program,,see below example? void main() { int i; for(i=0;i<10;i++) main(); } Then what is the output of the program?

6 Answers  


12344321 123 321 12 21 1 1 how i print this program??

5 Answers   DSR Management, Winit,


What does it mean when the linker says that _end is undefined?

0 Answers  


Why do we use null pointer?

0 Answers  


print ur name without using any semicolon in c/c++....

21 Answers   Bosch, TCS, Wipro,






The program to allow the characters from the input received and send this function to a function check if the characters between letters a to z is a function of y joins as the characters main and output to otherwise return to the original function of the y characters

0 Answers  


What does void main return?

0 Answers  


Is there any possibility to create customized header file with c programming language?

0 Answers  


i have a written test for microland please give me test pattern

0 Answers   Microland,


f() { int a=2; f1(a++); } f1(int c) { printf("%d", c); } c=?

7 Answers   Geometric Software,


C program to find all possible outcomes of a dice?

0 Answers  


Hai sir, I had planned to write the NIC scientific engineer exam , plz post the sample question......

0 Answers  


Categories