This C Program counts the number of occurrence of each character ignoring the case and prints them.
Here is a source code of the C program to count the number of occurrence of each character in a string ignoring their case and also prints them. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Count the Number of Occurrence of
* each Character Ignoring the Case of Alphabets
* & Display them
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct detail
{
char c;
int freq;
};
int main()
{
struct detail s[26];
char string[100], c;
int i = 0, index;
for (i = 0; i < 26; i++)
{
s[i].c = i + 'a';
s[i].freq = 0;
}
printf("Enter string: ");
i = 0;
do
{
fflush(stdin);
c = getchar();
string[i++] = c;
if (c == '\n')
{
break;
}
c = tolower(c);
index = c - 'a';
s[index].freq++;
} while (1);
string[i - 1] = '\0';
printf("The string entered is: %s\n", string);
printf("*************************\nCharacter\tFrequency\n*************************\n");
for (i = 0; i < 26; i++)
{
if (s[i].freq)
{
printf(" %c\t\t %d\n", s[i].c, s[i].freq);
}
}
return 0;
}
$ cc allcharfreq.c $ ./a.out Enter string: A quIck brOwn fox JumpEd over a lazy dOg The string entered is: A quIck brOwn fox JumpEd over a lazy dOg ************************* Character Frequency ************************* a 3 b 1 c 1 d 2 e 2 f 1 g 1 i 1 j 1 k 1 l 1 m 1 n 1 o 4 p 1 q 1 r 2 u 2 v 1 w 1 x 1 y 1 z 1
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Practice BCA MCQs
- Buy Computer Science Books
- Apply for C Internship