In this article, we’ll discuss how to count vowels and consonants in a string in the C# program.
Table of Contents
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:
- How to remove duplicate characters from a String in C#
- How to remove special characters from a string in C#
- C# program to count the occurrences of each character in a String
- Leap Year Program in C, C++, C#, JAVA, PYTHON, and PHP
- Bubble sort programs in C, C++, JAVA, and PYTHON
- Program to copy all elements of an array into another array
- Program to print prime numbers from 1 to N
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- Fibonacci sequence: Fibonacci series in C# (with examples)
- Program to Convert Fahrenheit to Celsius: Algorithm, Formula, and Code Examples
- Converting Celsius to Fahrenheit in C#
- Different Ways to Calculate Factorial in C# (with Full Code Examples)
- C# Tutorial
Latest posts by Shekh Ali (see all)
- Difference Between Array And ArrayList In C#: Choosing the Right Collection - May 28, 2024
- C# Program to Capitalize the First Character of Each Word in a String - February 21, 2024
- C# Program to Find the Longest Word in a String - February 19, 2024