[Solved] I want to count frequency or occurrence of a every letter in a string C program


Ok here is the rewrite, the original code is better but this one might be easier to understand:

#include <stdio.h>
#include <string.h>

int main()
{
   char cur_char;
   char string[100];
   int index = 0, count[255] = {0};

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


   while (string[index] != '\0')
   {
      char cur_char = string[index];

      // cur_char is a char but it acts as the index of the array like
      // if it was an unsigned short
      count[cur_char] = count[cur_char] + 1;

      index++;
   }

   for (index = 0; index < 255; index++)
   {
      if (count[index] != 0)
         printf("%c occurs %d times in the entered string.\n", index, count[index]);
   }

   return 0;
}

1

solved I want to count frequency or occurrence of a every letter in a string C program