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 are local variables c?

641


What is binary tree in c?

719


What is c++ used for today?

755


This is a variation of the call_me function in the previous question:call_me (myvar)int *myvar;{ *myvar += 5; }The correct way to call this function from main() will be a) call_me(myvar) b) call_me(*myvar) c) call_me(&myvar) d) expanded memory

860


What is hashing in c language?

727






Explain how can you avoid including a header more than once?

687


How to create struct variables?

689


What is the code in while loop that returns the output of given code?

1511


How is a pointer variable declared?

692


In C programming, what command or code can be used to determine if a number of odd or even?

708


Is it possible to pass an entire structure to functions?

651


What is a constant and types of constants in c?

695


What is a string?

756


What is difference between structure and union in c programming?

665


FORMATTED INPUT/OUTPUT functions are a) scanf() and printf() b) gets() and puts() c) getchar() and putchar() d) all the above

724