How to Find Vowels, Consonants, Digits, and Special Characters in a String

Metaverse News

A string is a sequence of characters. Those characters can be vowels, consonants, digits, or any special characters. In this article, you’ll learn how to find the total count of vowels, consonants, digits, and special characters in any given string.

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:

  1. Initialize variables to count the total number of vowels, consonants, digits, and special characters.
  2. Traverse the given string character by character.
  3. Check if the character belongs to the alphabet family, digit family, or special character family.
  4.  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.
  5. If the character belongs to the digit family, increment the variable’s value which stores the total count of digits in a string.
  6. 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:

Related: The Best Code-Along YouTube Channels to Learn Programming

#include <iostream>
using namespace std;
void countCharactersCategory(string s)
{
int totalSpecialCharacters = 0, totalDigits = 0, totalVowels = 0, totalConsonants = 0;
for (int i = 0; i < s.length(); i++)
{
char c = s[i];
// Alphabets family
if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
{
// Converting character to lower case
c = tolower(c);
// Vowels
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
totalVowels++;
}
// Consonants
else
{
totalConsonants++;
}
}
// Digits family
else if (c >= '0' && c <= '9')
{
totalDigits++;
}
// Special characters family
else
{
totalSpecialCharacters++;
}
}
cout << "Total no. of vowels in the given string: " << totalVowels << endl;
cout << "Total no. of consonants in the given string: " << totalConsonants << endl;
cout << "Total no. of digits in the given string: " << totalDigits << endl;
cout << "Total no. of special characters in the given string: " << totalSpecialCharacters << endl;
}
// Driver code
int main()
{
// Test case: 1
string s1 = "Welcome 2 #MUO";
cout << "Input string: " << s1 << endl;
countCharactersCategory(s1);
// Test case: 2
string s2 = "This Is @ InpuT String 2";
cout << "Input string: " << s2 << endl;
countCharactersCategory(s2);
return 0;
}

Output:

Input string: Welcome 2 #MUO
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 5
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 3
Input string: This Is @ InpuT String 2
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 12
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 6

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:

Related: Learning Python? Here’s How to Manipulate Strings


def countCharactersCategory(s):
totalSpecialCharacters = 0
totalDigits = 0
totalVowels = 0
totalConsonants = 0

for i in range(0, len(s)):
c = s[i]
# Alphabets family
if ( (c >= 'a' and c = 'A' and c = '0' and c <= '9'):
totalDigits += 1
# Special characters family
else:
totalSpecialCharacters += 1
print("Total no. of vowels in the given string: ", totalVowels)
print("Total no. of consonants in the given string: ", totalConsonants)
print("Total no. of digits in the given string: ", totalDigits)
print("Total no. of special characters in the given string: ", totalSpecialCharacters)

# Driver code
# Test case: 1
s1 = "Welcome 2 #MUO"
print("Input string: ", s1)
countCharactersCategory(s1)
# Test case: 2
s2 = "This Is @ InpuT String 2"
print("Input string: ", s2)
countCharactersCategory(s2)

Output:

Input string: Welcome 2 #MUO
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 5
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 3
Input string: This Is @ InpuT String 2
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 12
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 6

Related: How to Validate Strings Using Boolean Methods in Python

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:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
void countCharactersCategory(char s[])
{
int totalSpecialCharacters = 0, totalDigits = 0, totalVowels = 0, totalConsonants = 0;
for (int i = 0; i < strlen(s); i++)
{
char c = s[i];
// Alphabets family
if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
{
// Converting character to lower case
c = tolower(c);
// Vowels
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
totalVowels++;
}
// Consonants
else
{
totalConsonants++;
}
}
// Digits family
else if (c >= '0' && c <= '9')
{
totalDigits++;
}
// Special characters family
else
{
totalSpecialCharacters++;
}
}
printf("Total no. of vowels in the given string: %d ⁠n",totalVowels);
printf("Total no. of consonants in the given string: %d ⁠n",totalConsonants);
printf("Total no. of digits in the given string: %d ⁠n",totalDigits);
printf("Total no. of special characters in the given string: %d ⁠n",totalSpecialCharacters);
}
// Driver code
int main()
{
// Test case: 1
char s1[] = "Welcome 2 #MUO";
printf("Input string: %s
",s1);
countCharactersCategory(s1);
// Test case: 2
char s2[] = "This Is @ InpuT String 2";
printf("Input string: %s
",s2);
countCharactersCategory(s2);
return 0;
}

Output:

Input string: Welcome 2 #MUO 
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 5
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 3
Input string: This Is @ InpuT String 2
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 12
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 6

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:

<script>
function countCharactersCategory(s) {
var totalSpecialCharacters = 0, totalDigits = 0, totalVowels = 0, totalConsonants = 0;
for (var i = 0; i < s.length; i++) {
var c = s[i];
// Alphabets family
if ( (c >= "a" && c <= "z") || (c >= "A" && c <= "Z") ) {
// Converting character to lower case
c = c.toLowerCase();
// Vowels
if (c == "a" || c == "e" || c == "i" || c == "o" || c == "u") {
totalVowels++;
}
// Consonants
else {
totalConsonants++;
}
}
// Digits family
else if (c >= "0" && c <= "9") {
totalDigits++;
}
// Special characters family
else {
totalSpecialCharacters++;
}
}
document.write("Total no. of vowels in the given string: " + totalVowels + "<br>");
document.write("Total no. of consonants in the given string: " + totalConsonants + "<br>");
document.write("Total no. of digits in the given string: " + totalDigits + "<br>");
document.write("Total no. of special characters in the given string: " + totalSpecialCharacters + "<br>");
}
// Test case: 1
var s1 = "Welcome 2 #MUO";
document.write("Input string: " + s1 + "<br>");
countCharactersCategory(s1);
// Test case: 2
var s2 = "This Is @ InpuT String 2";
document.write("Input string: " + s2 + "<br>");
countCharactersCategory(s2);
</script>

Output:

Input string: Welcome 2 #MUO
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 5
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 3
Input string: This Is @ InpuT String 2
Total no. of vowels in the given string: 5
Total no. of consonants in the given string: 12
Total no. of digits in the given string: 1
Total no. of special characters in the given string: 6

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?

MUO – Feed