Search This Blog

Friday, February 25, 2011

How To Find Number of Vowels, Consonants, Digits, Words and Special Characters in a given String in C?


    /***********Prog. for counting different character**********/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char s[100];
 int b,c,d,e,i,l,p;
 b=c=d=e=0;
 clrscr();
 printf("\n\t Enter the string :=>");
 gets(s);
 l=strlen(s);
 for(i=0;s[i]!='\0';i++)
  {
   if((s[i]>=65&&s[i]<=90||s[i]>=97&&s[i]<=122))
   b++;
   if(s[i]>=48&&s[i]<=57)
   c++;
   if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
   d++;
      if (s[i]==' ')
   e++;
   }
   p=b-d;
   printf("\n\t No of vowels=%d \n",d);
   printf("\n\t No of consonants=%d \n ",p);
   printf("\n\t No of digits=%d \n",c);
   printf("\n\t no of words=%d \n ",e+1);
   printf("\n\t No of specal character=%d \n",l-b-c-e);
   getch();
}