C Program to Count the Occurrence of Each Character in String

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.

  1. /*
  2.  * C Program to Count the Number of Occurrence of
  3.  * each Character Ignoring the Case of Alphabets
  4.  * & Display them
  5.  */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9.  
  10. struct detail
  11. {
  12.     char c;
  13.     int freq;
  14. };
  15.  
  16. int main()
  17. {
  18.     struct detail s[26];
  19.     char string[100], c;
  20.     int i = 0, index;
  21.  
  22.     for (i = 0; i < 26; i++)
  23.     {
  24.        s[i].c = i + 'a';
  25.        s[i].freq = 0;
  26.     }
  27.     printf("Enter string: ");
  28.     i = 0;
  29.     do
  30.     {
  31.         fflush(stdin);
  32.         c = getchar();
  33.         string[i++] = c;
  34.         if (c == '\n')
  35.         {
  36.             break;
  37.         }
  38.         c = tolower(c);
  39.         index = c - 'a';
  40.         s[index].freq++;
  41.     } while (1);
  42.     string[i - 1] = '\0';
  43.     printf("The string entered is: %s\n", string);
  44.  
  45.     printf("*************************\nCharacter\tFrequency\n*************************\n");
  46.     for (i = 0; i < 26; i++)
  47.     {
  48.         if (s[i].freq)
  49.         {
  50.             printf("     %c\t\t   %d\n", s[i].c, s[i].freq);
  51.         }
  52.     }
  53.  
  54.     return 0;
  55. }

$ 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.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.