Count the Number of Vowels, Consonants, Digits, and Special characters in String

Count-the-number-of-vowels-consonants-digits-and-special-characters-in-string

Program Description:

Write a C Program to count the number of vowels, consonants, digits, and special characters in a string. The program should accept an input string from the user and count the number of Vowels, Consonants, Digits, and special characters(i.e other characters) in the input string.

📢 This Program is part of the C String Practice Programs series

Excepted Input and Output:

Input:

Output:

Number of Vowels are 2, consonants are 5, Digits are 4, and Special(Other) Characters are 3

Prerequisites of the Program:

You must know the basics of the strings and arrays in order to better understand the following program. Please go through these articles to learn more about strings and arrays.

Algorithm Count Number of vowels consonants digits and special characters in a string:

Let’s look at the step-by-step explanation/algorithm of the program.

  1. Create a Global constant named SIZE and initialize it with 100. This SIZE constant contains the max size of the character array (string)
  2. Declare the Input string with SIZE size. inputStr[SIZE];
  3. Prompt the user to provide the input string and update the inputStr string.
  4. Create four variables to count the number of vowels ( nVowels), number of consonants( nConsonants), number of digits( nDigits), and the number of special characters( nSpecialChars) and initialize with Zero(0).
  5. To count the vowels, consonants, digits, etc in the string, We need to iterate through all elements(characters) of the string and check each character.
  6. Start a For loop from i=0 continue till the inputStr[i] is not equal to NULL. – for (i=0; inputStr[i]; i++)
    • Count Vowels: Check if the inputStr[i] is equal to any vowel character i.e ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ ( We need to check both lower case and upper case characters).
      • So use the if(ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') condition.
      • If the above condition is True, Then ch i.e inputStr[i] is Vowel Character. So Increment the nVowels variable.
    • Count Consonants: Check if the inputStr[i]/ ch is a valid alphabet. As it is already not a Vowel, Then it must be a consonant.
      • User the if( (ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’)) condition
      • If the above condition is true, Then inputStr[i] / ch is a Consonant. So increment the nConsonant variable.
    • Count Digits: Similarly check if the ch is a digit using the if( (ch >= ‘0’ && ch <= ‘9’)) condition.
      • If this condition is true, The ch is a Digit. Increment the nDigits variable.
    • Count Other / Special Characters: If any of the above cases are not satisfied, Then the number must be other character or special character. Increment the nSpecialCharacters variable.
  7. Once the above for loop is completed, The nVowels, nConsonants, nDigits, and nSpecialCharacters contain the number of Vowels, Consonants, Digits, and Special characters in the input string.
  8. Display the results on the console.

Count Number of vowels consonants digits and special characters in string Program:

Convert the above logic to C Program.

Program Output:

Let’s compile and Run the Program.

count-number-of-vowels-consonants-digits-and-special-characters-in-string-program-output

As we can see from the above output, The program is properly counting the vowels, consonants, digits, etc.

Let’s try another example.

Count Number of vowels consonants digits and special characters in string using user-defined functions:

Let’s divide the above program into sub-functions where each function does a specific task. In this case, We can have three functions called isVowel(), isConsonants(), and isDigits() to check the vowels, consonants, and digits respectively.

As we have discussed in the functions tutorial, It is always recommended to divide the large program into functions. Functions improve debugging readability and modularity.

Here is the rewritten version of the above program with user-defined functions.

We have defined three user-defined functions(except the main() function) in the above program.

  1. The isVowel() Function
    • Prototype: int isVowel(char ch)
    • The isVowel() function takes a Formal argument i.e ch and checks if the ch is a Vowel.
    • It returns 1 if the ch is a Vowel, Otherwise, It returns Zero(0).
  2. The isConsonant() Function.
    • Prototype: int isConsonant(char ch)
    • The isConsonant() function also takes a formal argument it is a character( ch). And verifies if the ch is a Consonant.
    • It returns 1 if the ch is a consonant, Otherwise, It returns Zero(0).
  3. The isDigit() function.
    • Prototype: int isDigit(char ch)
    • The isDigit() function also similar to the above two functions and Returns True(1) if the ch is a Digit. Otherwise returns Zero(0).

Program Output:

Let’s Compile and Run the Program using GCC (Any compiler).

count-number-of-vowels-consonants-digits-and-special-characters-in-string-using-functions

In the input string Belief creates the actual fact 12345, We have 11 Vowels, 15 consonants, 5 Digits, and 5 Other characters.

Exercise:

Rewrite the program using the ctype.h functions like isalpha(), etc.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] Count the Number of Vowels, Consonants, Digits, and Special characters in String […]

Leave a Reply