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
What are multibyte characters?
What is c language in simple words?
Can we change the value of static variable in c?
How can my program discover the complete pathname to the executable from which it was invoked?
What do you mean by keywords in c?
What are the advantage of c language?
Why is c called c not d or e?
Why is c so important?
What is data types?
to find the closest pair
Hello. How to write a C program to check and display president party like if i type in the console "biden" and hit enter the output shoud be : "biden is democrat" and if i type "trump" and hit enter the output shoud be: "trump is republican"
What are header files in c programming?
Write a C program to help a HiFi’s Restaurant automate its breakfast billing system. Your assignment should implement the following items: a. Show the customer the different breakfast items offered by the HiFi’s Restaurant. b. Allow the customer to select more than one item from the menu. c. Calculate and print the bill to the customer. d. Produce a report to present your complete program and show more sample output. Assume that the HiFi’s Restaurant offers the following breakfast menu: Plain Egg $2.50 Bacon and Egg $3.45 Muffin $2.20 French Toast $2.95 Fruit Basket $3.45 Cereal $0.70 Coffee $1.50 Tea $1.80
to print the salary of an employee according to follwing calculation: Allowances:HRA-20% of BASIC,DA-45% of BASIC,TA-10%. Deductions:EPF-8% of BASIC,LIC-Rs.200/-Prof.Tax:Rs.200/- create c language program?
Are c and c++ the same?