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++
#include <bits/stdc++.h>
using namespace std;
void countCharacterType(string str)
{
int vowels = 0, consonant = 0, specialChar = 0,
digit = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str[i];
if ( (ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ) {
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;
}
int main()
{
string str = "geeks for geeks121";
countCharacterType(str);
return 0;
}
Java
import java.io.*;
public class GFG {
static void countCharacterType(String str)
{
int vowels = 0, consonant = 0, specialChar = 0,
digit = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if ( (ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z') ) {
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);
}
static public void main (String[] args)
{
String str = "geeks for geeks121";
countCharacterType(str);
}
}
Python3
def countCharacterType(str):
vowels = 0
consonant = 0
specialChar = 0
digit = 0
for i in range(0, len(str)):
ch = str[i]
if ( (ch >= 'a' and ch <= 'z') or
(ch >= 'A' and ch <= 'Z') ):
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)
str = "geeks for geeks121"
countCharacterType(str)
C#
using System;
using System.Globalization;
class GFG {
static void countCharacterType(string str)
{
int vowels = 0, consonant = 0,
specialChar = 0, digit = 0;
for (int i = 0; i < str.Length; i++) {
char ch = str[i];
if ((ch >= 'a' && ch <= 'z') ||
(ch >= 'A' && ch <= 'Z')) {
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);
}
static public void Main()
{
string str = "geeks for geeks121";
countCharacterType(str);
}
}
Javascript
<script>
function countCharacterType(str) {
var vowels = 0,
consonant = 0,
specialChar = 0,
digit = 0;
for (var i = 0; i < str.length; i++) {
var ch = str[i];
if ((ch >= "a" && ch <= "z") ||
(ch >= "A" && ch <= "Z")) {
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>");
}
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)
Last Updated :
20 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...