C program to count the number of vowels,consonants,digits,white spaces and special characters in a given string

C program to count the number of vowels, consonants, digits, white spaces & special characters in a given string

This C program is to count the number of vowels, consonants, digits, white spaces & special characters in a given string.For example, string “code 21” will have 2 consonants(c,d),2 vowels(o,e), 2 digits(2,1), 1 whitespace and 1 special character(;).

Logic

We just use a for loop and for each parameter we keep an if condition along with a counter for each parameter.

Dry Run of the Program

Take input string str.Let us take str=code 21;

1st iteration for(i=0;str[i]!=’\0′;i++) i.e. for(i=0;str[0]!=’\0′;i++)

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’) false

else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’))  true str[0]=’c’

consonants++ hence consonants=1

2nd iteration for(i=1;str[i]!=’\0′;i++) i.e. for(i=1;str[1]!=’\0′;i++)

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’) true as str[1]=’o’

vowels++; hence vowels=1

3rd iteration for(i=2;str[i]!=’\0′;i++) i.e. for(i=2;str[2]!=’\0′;i++)

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’) false

else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’))  true as str[2]=’d’

consonants++ hence consonants=2

4th iteration for(i=3;str[i]!=’\0′;i++) i.e. for(i=3;str[3]!=’\0′;i++)

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’) true as str[3]=’e’

vowels++ hence vowels=2

else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’))  false

5th iteration for(i=4;str[i]!=’\0′;i++) i.e. for(i=4;str[4]!=’\0′;i++)

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’) false

else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’))  false

else if(str[i]>=’0′ && str[i]<=’9′) false

else if (str[i]==’ ‘) true as str[4]=’ ‘ 

spaces++ hence spaces=1

6th iteration for(i=5;str[i]!=’\0′;i++) i.e. for(i=5;str[5]!=’\0′;i++)

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’) false

else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’))  false

else if(str[i]>=’0′ && str[i]<=’9′) true as str[5]=2

digits++ hence digits=1

7th iteration for(i=6;str[i]!=’\0′;i++) i.e. for(i=6;str[6]!=’\0′;i++)

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’) false

else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’))  false

else if(str[i]>=’0′ && str[i]<=’9′) true as str[6]=1

digits++ hence digits=2

8th iteration for(i=7;str[i]!=’\0′;i++) i.e. for(i=7;str[7]!=’\0′;i++)

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’) false

else if((str[i]>=’a’&& str[i]<=’z’) || (str[i]>=’A’&& str[i]<=’Z’))  false

else if(str[i]>=’0′ && str[i]<=’9′) false

else true as str[7]=’;’

specialCharacters++ hence specialCharacters=1

Hence output is consonants=2, vowels=2, digits=2, spaces=1, specialCharacters=1 for string code 21;

Program

#include<stdio.h>

void main()
{
    char str[200];
    int i,vowels=0,consonants=0,digits=0,spaces=0,specialCharacters=0;
    

    printf("Enter a string\n");
    gets(str);
    for(i=0;str[i]!='\0';i++)
    {
        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')
        {
            vowels++;
        }
        else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z'))
        {
            consonants++;
        }
        else if(str[i]>='0' && str[i]<='9')
        {
            digits++;
        }
        else if (str[i]==' ')
        {
            spaces++;
        }
        else
        {
            specialCharacters++;
        }
    }

    printf("\nVowels = %d",vowels);
    printf("\nConsonants = %d",consonants);
    printf("\nDigits = %d",digits);
    printf("\nWhite spaces = %d",spaces);
    printf("\nSpecial characters = %d",specialCharacters);
    
}

Output

Share Me!