Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sabique/0f7ec4ec93db8520cdb758cb833adb36 to your computer and use it in GitHub Desktop.
Save sabique/0f7ec4ec93db8520cdb758cb833adb36 to your computer and use it in GitHub Desktop.
Program to count vowels, consonant, digits and special characters in string. -- Given a string and the task is to count vowels, consonant, digits and special character in string. Special character also contains the white space.
/*
Program to count vowels, consonant, digits and special characters in string.
Given a string and the task is to count vowels, consonant, digits and special character in string. Special character also contains the white space.
Problem Link: https://www.geeksforgeeks.org/program-count-vowels-consonant-digits-special-characters-string/
Solution Time Complexity: O(n) -> Where 'n' is the length of the string.
*/
using System;
public class Program
{
public static void Main()
{
int vowel = 0, consonant = 0, numeric = 0, special = 0;
//Assign the values to the string
string sentence = " A1 B@ d adc";
string lower = sentence.ToLower();
//Iterate the characters of lower string
foreach(char c in lower.ToCharArray())
{
//Check if the character is a letter
if((c >= 'a' && c <= 'z'))
{
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
//Increase the counter of vowel by 1
vowel++;
}
else
{
//Increase the counter of consonant by 1
consonant++;
}
}
//Check if the character is numeric
else if(c >= '0' && c <= '9')
{
//Increase the counter of numeric by 1
numeric++;
}
//The charcater is a special character
else
{
//Increase the counter of special by 1
special++;
}
}
//Print the counter result
Console.WriteLine("Vowels: {0} \nConsonant: {1} \nNumeric: {2} \nSpecial: {3}", vowel, consonant, numeric, special);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment