Examples to Understand the Problem

Example 1: Let the given string be “Welcome 2 #MUO”.

s = “Welcome 2 #MUO”

There are 5 vowels in the given string: e, o, e, U, and O.

There are 5 consonants in the given string: W, l, c, m, and M.

There is 1 digit in the given string: 2.

There are 3 special characters in the given string: # and two white spaces.

Example 2: Let the given string be “This is @ inpuT  String 2”.

s = “This Is @ InpuT String 2”

There are 5 vowels in the given string: i, I, I, u, and i.

There are 12 consonants in the given string: T, h, s, s, n, p, T, S, t, r, n, and g.

There is 1 digit in the given string: 2.

There are 6 special characters in the given string: @ and five white spaces.

Note: White space is treated as a special character in the string.

Approach to Count Vowels, Consonants, Digits, and Special Characters in a String

You can find the total number of vowels, consonants, digits, and special characters in a string by following the approach below:

Initialize variables to count the total number of vowels, consonants, digits, and special characters. Traverse the given string character by character. Check if the character belongs to the alphabet family, digit family, or special character family.  If the character belongs to the alphabet family, first convert the character to lowercase and then check if the character is a vowel or a consonant. If the character is a vowel, increment the variable’s value which stores the total count of vowels in a string. Else if the character is a consonant, increment the variable’s value which stores the total count of consonants in a string. If the character belongs to the digit family, increment the variable’s value which stores the total count of digits in a string. If the character belongs to the special character family, increment the variable’s value which stores the total count of special characters in a string.

C++ Program to Count Vowels, Consonants, Digits, and Special Characters in a String

Below is the C++ program to count vowels, consonants, digits, and special characters in a string:

Output:

Python Program to Count Vowels, Consonants, Digits, and Special Characters in a String

Below is the Python program to count vowels, consonants, digits, and special characters in a string:

Output:

C Program to Count Vowels, Consonants, Digits, and Special Characters in a String

Below is the C program to count vowels, consonants, digits, and special characters in a string:

Output:

JavaScript Program to Count Vowels, Consonants, Digits, and Special Characters in a String

Below is the JavaScript program to count vowels, consonants, digits, and special characters in a string:

Output:

If you want to have a look at the complete source code used in this article, here’s the GitHub repository.

Practice String Problems for Your Interviews

String problems are one of the most asked questions in coding contests and interviews. Understand the basics of strings and practice famous problems to become a better engineer.

Removing duplicate characters from a string, finding the maximum occurring character in a string, and checking if a string is a palindrome are some of the famous string problems.

Why not try these problems too?