Search This Blog

Wednesday, 2 March 2022

B7. C Program to read a string and to find the number of alphabets, digits, vowels, consonants, spaces and special characters.

 /* B7. C Program to read a string and to find the number of alphabets, digits, vowels,
consonants, spaces and special characters. */

#include <stdio.h>
 
void main()
{
    char   str[100];
    int i, digit, vowel, consonant, splchar, spaces;
    clrscr();

    digit = vowel = consonant = splchar = spaces = 0;

    printf("Enter a string: ");
    gets(str);

    for(i=0;str[i]!=NULL;i++)
    {

    if(str[i]>='0' && str[i]<='9')
        digit++;
    else if(str[i]==' ')
        spaces++;
    else if(str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]== 'O' || str[i]=='U' ||
    str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u')

        vowel++;
    else if((str[i]>='A' && str[i]<='Z')||(str[i]>='a' && str[i]<='z'))
        consonant++;
    else
        splchar++;
    }
 
   printf("\nDigits: %d \nVowels: %d \nSpaces: %d \nConsonants: %d \nSpecial Characters: %d",digit,vowel,spaces, consonant,splchar);
   getch();
}

No comments :

Post a Comment

FREE Hit Counters