Contact Learn C
Copy

Program 90:To count vowels,consonants,digits and special characters

Program 90:
 
#include<stdio.h>
#include<string.h>
main()
{
int i,vowel=0,consonant=0,special=0,letters=0,digits=0;
char str[100];
printf("Enter string 1\n");
gets(str);

for(i=0;i<strlen(str);i++)
{
    if(str[i]=='A'||str[i]=='a'||str[i]=='E'||str[i]=='e'||
    str[i]=='I'||str[i]=='i'||str[i]=='O'||str[i]=='o'||str[i]=='U'||str[i]=='u')
    {
        vowel++;
    }
    
    else if(str[i]>=0&&str[i]<=47||str[i]>=58&&str[i]<=64||
    str[i]>=91&&str[i]<=96||str[i]>=123&&str[i]<=127)
    {
        special++;
    }

    else if(str[i]>'0'&&str[i]<'9')
    {
        digits++;
    }
}
printf("vowel count=%d\n",vowel);
printf("consonant count=%d\n",strlen(str)-vowel-special-digits);
printf("digits count=%d\n",digits);
printf("Special Characters=%d\n",special);

}
Explanation:

  1. This program starts with initializing :
    • i → used as helping variable
    • str → To store input from user
    • vowel,consonant,special,digit → To store number of vowels,consonants etc
  2. printf("Enter string 1\n");
    gets(str);
    To take input from user 
  3. for(i=0;i<strlen(str);i++)
    {
    To traverse from 0 to end of string one by one 
  4. if(str[i]=='A'||str[i]=='a'||str[i]=='E'||str[i]=='e'||
        str[i]=='I'||str[i]=='i'||str[i]=='O'||str[i]=='o'||str[i]=='U'||str[i]=='u')
        {
            vowel++;
        }
    To check whether the character is A/a,E/e,I/i,O/o,U/u and if the character is anyone of them then vowel is incremented by 1.Finally number of vowels are stored in vowel
  5. else if(str[i]>=0&&str[i]<=47||str[i]>=58&&str[i]<=64||
        str[i]>=91&&str[i]<=96||str[i]>=123&&str[i]<=127)
        {
            special++;
        }
    To understand this refer ASCII Sheet as the characters which are not alphabets,numbers are considered as special characters so the character which lies between 0&47 or 58&64 or 91&96 or 123&127 are special characters (space included) from ASCII Sheet. If the character lies in between these special is incremented by 1
  6. else if(str[i]>'0'&&str[i]<'9')
        {
            digits++;
        }
    If character lies in between 0 and 9 then digit is incremented by 1. 
  7. printf("vowel count=%d\n",vowel);
    printf("consonant count=%d\n",strlen(str)-vowel-special-digits);
    printf("digits count=%d\n",digits);
    printf("Special Characters=%d\n",special);
    Finally all vowels,digit,special are printed and we get consonant by calculating stringlength-vowel-special-digits.
    Example Hello World has
    vowel=e,o,o=3
    digit=0
    special=(space)=1
    consonant=11-3-0-1=7


 Output:

To count vowels,consonants,digits and special characters
Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide