Q:

C program to find the frequency of characters in a string

0

C program to find the frequency of characters in a string

C program to find the frequency of characters in a string: This program counts the frequency of characters in a string, i.e., which character is present how many times in the string. For example, in the string "code" each of the characters 'c,' 'd,' 'e,' and 'o' has occurred one time. Only lower case alphabets are considered, other characters (uppercase and special characters) are ignored. You can modify this program to handle uppercase and special symbols.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include 
#include 

int main()
{
   char string[100];
   int c = 0, count[26] = {0}, x;

   printf("Enter a string\n");
   gets(string);

   while (string[c] != '\0') {
   /** Considering characters from 'a' to 'z' only and ignoring others. */

      if (string[c] >= 'a' && string[c] <= 'z') {
         x = string[c] - 'a';
         count[x]++;
      }

      c++;
   }

   for (c = 0; c < 26; c++)
         printf("%c occurs %d times in the string.\n", c + 'a', count[c]);

   return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now