Thursday, September 20, 2012

c program to find frequency of characters in a string


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

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

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

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

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

      c++;
   }

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

   return 0;
}

2 comments:

  1. Great work. I am highly obliged that you are sharing such info with us. I would appreciate if you will post on daily basis and that too with good typical info which i didn't get from anywhere. I will be enrolling in http://www.wiziq.com/course/6314-learn-c-programming-language-low-priced-student-edition I really appreciate your work

    ReplyDelete
  2. Nice program... You have a nice collection of C programs for beginners. How ever it would be better if you cam add some comments in your code like here C Program find frequency of characters

    ReplyDelete