C# program to count vowels and consonants in a string

In this article, we’ll discuss how to count vowels and consonants in a string in the C# program.

Method1: C# program to count vowels and consonants in a string

Let’s start with a simple C# program that count vowels and consonants in a string. Below is the complete code, and we’ll break it down step by step.

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter a string:");
        string input = Console.ReadLine();

        int vowelCount = CountVowels(input);
        int consonantCount = CountConsonants(input);

        Console.WriteLine($"Vowel count: {vowelCount}");
        Console.WriteLine($"Consonant count: {consonantCount}");
    }

    static int CountVowels(string str)
    {
        int count = 0;
        foreach (char c in str.ToLower())
        {
            if ("aeiou".Contains(c))
            {
                count++;
            }
        }
        return count;
    }

    static int CountConsonants(string str)
    {
        int count = 0;
        foreach (char c in str.ToLower())
        {
            if (char.IsLetter(c) && !"aeiou".Contains(c))
            {
                count++;
            }
        }
        return count;
    }
}

Output:

Enter a string:
abcdefghijklmnopqrstuvwxyz
Vowel count: 5
Consonant count: 21

Code Explanation:

  • The program starts by prompting the user to enter a string. The input is then stored in the input variable.
  • The CountVowels method counts the number of vowels in the given string. It converts the input string to lowercase to ensure case-insensitive counting.
  • Similarly, the CountConsonants method counts the number of consonants. It checks if a character is a letter and not a vowel.
  • After counting, the program displays the results to the user.

Method2: Count vowels and consonants in a string using LINQ

Let’s add another method to count vowels and consonants in a string using a slightly different approach. This time, we’ll use LINQ (Language Integrated Query) to demonstrate an alternative method.

using System;
using System.Linq;

class Program
{
    static void Main()
    {
        Console.WriteLine("Enter a string:");
        string input = Console.ReadLine();

        int vowelCount = CountVowels(input);
        int consonantCount = CountConsonants(input);

        Console.WriteLine($"Vowel count: {vowelCount}");
        Console.WriteLine($"Consonant count: {consonantCount}");
    }

    static int CountVowels(string str)
    {
        // Using LINQ to count vowels
        int vowelCount = str.ToLower().Count(c => "aeiou".Contains(c));
        return vowelCount;
    }

    static int CountConsonants(string str)
    {
        // Using LINQ to count consonants
        int consonantCount = str.ToLower().Count(c => char.IsLetter(c) && !"aeiou".Contains(c));
        return consonantCount;
    }
}

Compile and run the above program to test it with different strings. This approach allows you to see the results for various inputs.

Output:

Enter a string:
This is a simple Hello World.
Vowel count: 8
Consonant count: 15

Conclusion

  • Counting vowels and consonants in a string is a basic but powerful skill in programming. This article has provided a clear and practical method for implementing such functionality. 

Feel free to modify and expand the code to suit your needs. Happy coding!

Programs you might also like:

Shekh Ali
4 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments