C# Program to Count Number of Vowels and Consonants in a String

This is a C# Program to count number of vowels and consonants from a given string.

Problem Description

This C# Program Counts number of Vowels and consonants from a given String.

Problem Solution

Here program is used to find the number of vowels and consonants present in the given sentence. The vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, The remaining letters are consonants.

Program/Source Code

Here is source code of the C# Program to Count number of Vowels and consonants from a given String. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Count number of Vowels and consonants from a given String
 */
using System;
class program
{
    public static void Main()
    {
        char[] sentence = new char[100];
 
        int i, vowels = 0, consonants = 0, special = 0, n;
        Console.WriteLine("Enter the Length of the sentence  \n");
        n = int.Parse(Console.ReadLine());
        for (i = 0; i < n; i++)
        {
            sentence[i] = Convert.ToChar(Console.Read());
        }
        for (i = 0; sentence[i] != '\0'; i++)
        {
            if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i] ==
            'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
            (sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
            'I' || sentence[i] == 'O' || sentence[i] == 'U'))
            {
                vowels = vowels + 1;
            }
            else
            {
                consonants = consonants + 1;
            }
            if (sentence[i] == 't' || sentence[i] == '\0' || sentence[i] == ' ')
            {
                special = special + 1;
            }
        }
 
        consonants = consonants - special;
        Console.WriteLine("No. of vowels {0}", vowels);
        Console.WriteLine("No. of consonants {0}", consonants);
        Console.ReadLine();
        Console.ReadLine();
    }
}
Program Explanation

In this C# program, we are reading the length of the sentence using ‘n’ variable. Using for loop convert the string to each character using sentence[] array variable. The vowels are ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, the remaining letters are consonants. For loop is used to count the number of vowels, consonants and special characters present in the string.

advertisement
advertisement

If else condition statement is used to check the value of ‘i’ variable is equal to vowel characters lower case or upper case using Logical OR operators. If the condition is true then execute the statement and increment the value of ‘vowels’ variable by one. Otherwise, if the condition is false then execute the else statement and increment the value of ‘consonants’ variable by one.

Otherwise, if the condition is false then execute the else statement. Check the string present in the sentence has special character using Logical OR operators. If the condition is true then execute the statement and increment the value of ‘special’ variable by 1. Print the number of vowels and consonants in the string.

Runtime Test Cases
 
Enter the Length of the sentence
3
san
No. of vowels 1
No. of consonants 2

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.