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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What would the following code segment printint k = 8;docout << "k = " << k << " ";while k++ < 5; a) 13 b) 5 c) 8 d) pointers

668


Write a program to check prime number in c programming?

586


How are strings stored in c?

582


What is the difference between char array and char pointer?

520


What is the explanation for prototype function in c?

560






What is c system32 taskhostw exe?

580


please explain clearly about execution of c program in detail,in which stage are the printf sacnf getting into exeecutable code

1701


Explain continue keyword in c

576


Why malloc is faster than calloc?

581


Some coders debug their programs by placing comment symbols on some codes instead of deleting it. How does this aid in debugging?

649


Which one would you prefer - a macro or a function?

593


What is the best organizational structure?

635


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

972


Explain enumerated types in c language?

598


Explain how do you list a file’s date and time?

615