C Program to Count Consonants, Vowels, Digits, Spaces in String

Here we will learn about how to create a program in C that asks from the user to enter any string at run-time, to count total number of:

  • All characters
  • Uppercase characters
  • Lowercase characters
  • Digits
  • Vowels
  • Space characters
  • Words
  • Lines
  • Consonants

present inside the given string. Here is the program:

int main()
    char str[100];
    int i, count=0, countChar=0;
    int countUpper=0, countLower=0, countDigit=0, countVowel=0;
    int countSpace=0, countWord=0, countLine=0, countConsonant=0;
    printf("Enter any string: ");
    for(i=0; str[i]!='\0'; i++)
        if(str[i]>=65 && str[i]<=90)

        else if(str[i]>=97 && str[i]<=122)
        else if(str[i]>=48 && str[i]<=57)
        else if(str[i]==' ')
        else if(str[i]=='\n')
        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')
        if(str[i]!=' ')
            if(count!=0 && str[i+1]!='\0')
            count = 0;
    countConsonant = (countUpper+countLower) - countVowel;
    printf("\nTotal Character = %d", countChar);
    printf("\nTotal Uppercase Character = %d", countUpper);
    printf("\nTotal Lowercase Character = %d", countLower);
    printf("\nTotal Vowel = %d", countVowel);
    printf("\nTotal Consonant = %d", countConsonant);
    printf("\nTotal Digit = %d", countDigit);
    printf("\nTotal Space = %d", countSpace);
    printf("\nTotal Word = %d", countWord+1);
    printf("\nTotal Line = %d", countLine);
    return 0;

The program was written under Code::Blocks IDE, therefore here is the sample run after successful build and run:

c count vowels consonants spaces digits

Now supply any string say Hello, this is program no.62 on codescracker and press ENTER key to see the following output:

count digits vowels consonants spaces c

Program Explained

  • Declare and initialize 0 to all the variables responsible to count total number of
    • all characters
    • uppercase characters
    • lowercase characters
    • vowels
    • consonants
    • digits
    • spaces
    • words
    • lines
  • say
    • countChar
    • countUpper
    • countLower
    • countVowel
    • countConsonant
    • countDigit
    • countSpace
    • countWord
    • countLine
  • Now get the string input from user at run-time
  • Create a for loop that runs from 0 to the last character of the string
  • As the null terminated character ('\0') inside any string is the last character
  • Therefore, we have to check for each and every character present inside the string from 0th place to before the null terminated character
  • Inside the for loop, create if block to check what the present character is, that is if it is uppercase, then increment the value of variable responsible for uppercase character count say countUpper and so on
  • The ASCII code for A-Z (A to Z) is 65-90 (65 to 90). That is ASCII code of A is 65, ASCII code of B is 66, ... and ASCII code of Z is 90
  • The ASCII code for a-z is 97-122
  • The ASCII code for 0-9 is 48-57
  • After exiting from the for loop, print the value of all the character one by one

C Online Test

« Previous Program Next Program »

Follow/Like Us on Facebook

Subscribe Us on YouTube