C Program to Count the Number of Occurrence of each Character Ignoring the Case of Alphabets & Display them

«
»
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. }

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

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn