Thursday, July 21, 2011

Program to count occourence of each character in a given string

/*Serial No.112     [swami89.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],comp;    //here in place of str[50] if I write *str then
int n,i,j,count;        //program also works. [TO CHECK WHY ???]
clrscr();
printf("Enter the string==>");
scanf("%s",str);
n=strlen(str);
for(i=0;i<n;i++)
    {
    comp=str[i];
    if(comp!=' ')
      {
      count=0;
      for(j=0;j<n;j++)
        {
        if(comp==str[j])
          {
          count+=1;
          str[j]=' ';
          }
        }
      }
    else
      continue;
    printf("%c occurs %d times\n",comp,count);
    }
getch();
}

No comments:

Post a Comment

If you have any doubt, feel free to ask...