Program to count vowels, consonant, digits and special characters in string. Last Updated : 20 Feb, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string and the task is to count vowels, consonant, digits and special character in string. Special character also contains the white space.Examples: Input : str = "geeks for geeks121" Output : Vowels: 5 Consonant: 8 Digit: 3 Special Character: 2 Input : str = " A1 B@ d adc" Output : Vowels: 2 Consonant: 4 Digit: 1 Special Character: 6 C++ // Program to count vowels, consonant, digits and // special character in a given string. #include <bits/stdc++.h> using namespace std; // Function to count number of vowels, consonant, // digitsand special character in a string. void countCharacterType(string str) { // Declare the variable vowels, consonant, digit // and special characters int vowels = 0, consonant = 0, specialChar = 0, digit = 0; // str.length() function to count number of // character in given string. for (int i = 0; i < str.length(); i++) { char ch = str[i]; if ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ) { // To handle upper case letters ch = tolower(ch); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowels++; else consonant++; } else if (ch >= '0' && ch <= '9') digit++; else specialChar++; } cout << "Vowels: " << vowels << endl; cout << "Consonant: " << consonant << endl; cout << "Digit: " << digit << endl; cout << "Special Character: " << specialChar << endl; } // Driver function. int main() { string str = "geeks for geeks121"; countCharacterType(str); return 0; } Java // Java Program to count vowels, consonant, digits and // special character in a given string import java.io.*; public class GFG { // Function to count number of vowels, consonant, // digitsand special character in a string. static void countCharacterType(String str) { // Declare the variable vowels, consonant, digit // and special characters int vowels = 0, consonant = 0, specialChar = 0, digit = 0; // str.length() function to count number of // character in given string. for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ( (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ) { // To handle upper case letters ch = Character.toLowerCase(ch); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowels++; else consonant++; } else if (ch >= '0' && ch <= '9') digit++; else specialChar++; } System.out.println("Vowels: " + vowels); System.out.println("Consonant: " + consonant); System.out.println("Digit: " + digit); System.out.println("Special Character: " + specialChar); } // Driver function. static public void main (String[] args) { String str = "geeks for geeks121"; countCharacterType(str); } } // This code is contributed by vt_m. Python3 # Python3 Program to count vowels, # consonant, digits and special # character in a given string. # Function to count number of vowels, # consonant, digits and special # character in a string. def countCharacterType(str): # Declare the variable vowels, # consonant, digit and special # characters vowels = 0 consonant = 0 specialChar = 0 digit = 0 # str.length() function to count # number of character in given string. for i in range(0, len(str)): ch = str[i] if ( (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z') ): # To handle upper case letters ch = ch.lower() if (ch == 'a' or ch == 'e' or ch == 'i' or ch == 'o' or ch == 'u'): vowels += 1 else: consonant += 1 elif (ch >= '0' and ch <= '9'): digit += 1 else: specialChar += 1 print("Vowels:", vowels) print("Consonant:", consonant) print("Digit:", digit) print("Special Character:", specialChar) # Driver function. str = "geeks for geeks121" countCharacterType(str) # This code is contributed by # Smitha Dinesh Semwal C# // Program to count vowels, consonant, // digits and special character in // a given string using System; using System.Globalization; class GFG { // Function to count number of // vowels, consonant, digitsand // special character in a string. static void countCharacterType(string str) { // Declare the variable vowels, consonant, // digit and special characters int vowels = 0, consonant = 0, specialChar = 0, digit = 0; // str.length() function to count number of // character in given string. for (int i = 0; i < str.Length; i++) { char ch = str[i]; if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) { // To handle upper case letters ch = char.ToLower(ch); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') vowels++; else consonant++; } else if (ch >= '0' && ch <= '9') digit++; else specialChar++; } Console.WriteLine("Vowels: " + vowels); Console.WriteLine("Consonant: " + consonant); Console.WriteLine("Digit: " + digit); Console.WriteLine("Special Character: " + specialChar); } // Driver function. static public void Main() { string str = "geeks for geeks121"; countCharacterType(str); } } // This code is contributed by vt_m. JavaScript <script> // Program to count vowels, consonant, // digits and // special character in a given string. // Function to count number of vowels, consonant, // digitsand special character in a string. function countCharacterType(str) { // Declare the variable vowels, // consonant, digit // and special characters var vowels = 0, consonant = 0, specialChar = 0, digit = 0; // str.length() function to count number of // character in given string. for (var i = 0; i < str.length; i++) { var ch = str[i]; if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z")) { // To handle upper case letters ch = ch.toLowerCase(); if (ch == "a" || ch == "e" || ch == "i" || ch == "o" || ch == "u") vowels++; else consonant++; } else if (ch >= "0" && ch <= "9") digit++; else specialChar++; } document.write("Vowels: " + vowels + "<br>"); document.write("Consonant: " + consonant + "<br>"); document.write("Digit: " + digit + "<br>"); document.write("Special Character: " + specialChar + "<br>"); } // Driver function. var str = "geeks for geeks121"; countCharacterType(str); </script> Output: Vowels: 5 Consonant: 8 Digit: 3 Special Character: 2 Time Complexity: O(N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to count vowels, consonant, digits and special characters in string. D dharmendra_kumar Follow Improve Article Tags : Misc Strings DSA Basic Coding Problems vowel-consonant CBSE - Class 11 +2 More Practice Tags : MiscStrings Similar Reads C program to count number of vowels and consonants in a String Given a string and write a C program to count the number of vowels and consonants in this string. Examples: Input: str = "geeks for geeks" Output: Vowels: 5 Consonants: 8 Input: str = "abcdefghijklmnopqrstuvwxyz" Output: Vowels: 5 Consonants: 21Using For LoopsTake the string as inputTake each charac 4 min read Program to find if a character is vowel or Consonant Given a character, check if it is vowel or consonant. Vowels are 'a', 'e', 'i', 'o' and 'u'. All other characters ('b', 'c', 'd', 'f' ....) are consonants. Examples: Input : x = 'c' Output : Consonant Input : x = 'u' Output : Vowel We check whether the given character matches any of the 5 vowels. If 12 min read Consonant and Vowel group check in String with Wildcard characters Given a string S which is composed of lowercase alphabets and wildcard characters i.e. '?'. Here, '?' can be replaced by any of the lowercase alphabets, the task is to return 'True' if there are more than 3 consonants together or more than 5 vowels together else 'False'. Examples: Input: S = aeioup? 5 min read Lexicographically first alternate vowel and consonant string Given a string str. The problem is to rearrange characters of the given string such that the vowels and consonants occupy alternate position and the string so formed should be lexicographically (alphabetically) smallest. If string can not be rearranged in desired way, print âno such stringâ. Example 12 min read Make all characters in given string equal by changing vowel into consonant and vice versa Given a string str containing lowercase characters, the task is to find the minimum number of operations needed to make all the characters equal. In one operation, any consonant can be converted to any vowel or any vowel can be converted to any consonant. Examples: Input: str = "banana"Output: 3Expl 14 min read Like