Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to check for a string that contains all vowels
A C# program to check if a string contains all vowels involves examining the string to identify which vowels (a, e, i, o, u) are present. This is useful for word games, text analysis, or linguistic applications where you need to verify vowel completeness.
Syntax
The basic approach uses LINQ methods to filter and check for vowels −
var vowels = str.Where(ch => "aeiouAEIOU".Contains(ch)).Distinct();
To check if all five vowels are present −
bool hasAllVowels = vowels.Count() == 5;
Using LINQ to Find All Vowels
This approach uses the Where() method to filter vowels and Distinct() to get unique vowels from the string −
using System;
using System.Linq;
public class Program {
public static void Main() {
string str = "the quick brown fox jumps over the lazy dog";
var res = str.Where(chk => "aeiouAEIOU".Contains(chk)).Distinct();
if (!res.Any())
Console.WriteLine("No vowels!");
Console.WriteLine("Vowels found in the string:");
foreach (var vowel in res)
Console.WriteLine("Your phrase contains vowel = {0}", vowel);
Console.WriteLine("\nTotal unique vowels: " + res.Count());
Console.WriteLine("Contains all vowels: " + (res.Count() == 5));
}
}
The output of the above code is −
Vowels found in the string: Your phrase contains vowel = e Your phrase contains vowel = u Your phrase contains vowel = i Your phrase contains vowel = o Your phrase contains vowel = a Total unique vowels: 5 Contains all vowels: True
Using HashSet for Efficient Checking
This method uses a HashSet to track which vowels are found, providing better performance for longer strings −
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
string str = "Education is important";
HashSet<char> vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
HashSet<char> foundVowels = new HashSet<char>();
foreach (char c in str.ToLower()) {
if (vowels.Contains(c)) {
foundVowels.Add(c);
}
}
Console.WriteLine("String: " + str);
Console.WriteLine("Found vowels: " + string.Join(", ", foundVowels));
Console.WriteLine("Contains all vowels: " + (foundVowels.Count == 5));
if (foundVowels.Count == 5) {
Console.WriteLine("This is a pangram sentence for vowels!");
}
}
}
The output of the above code is −
String: Education is important Found vowels: e, d, u, c, a, t, i, o, n Contains all vowels: True This is a pangram sentence for vowels!
Creating a Reusable Method
Here's a complete method that can be reused to check any string for vowel completeness −
using System;
using System.Linq;
public class VowelChecker {
public static bool ContainsAllVowels(string input) {
if (string.IsNullOrEmpty(input)) return false;
var vowels = "aeiou";
var foundVowels = input.ToLower()
.Where(c => vowels.Contains(c))
.Distinct()
.Count();
return foundVowels == 5;
}
public static void Main() {
string[] testStrings = {
"the quick brown fox jumps over the lazy dog",
"Hello World",
"Programming is fun",
"Facetious"
};
foreach (string test in testStrings) {
bool result = ContainsAllVowels(test);
Console.WriteLine($"'{test}' contains all vowels: {result}");
}
}
}
The output of the above code is −
'the quick brown fox jumps over the lazy dog' contains all vowels: True 'Hello World' contains all vowels: False 'Programming is fun' contains all vowels: False 'Facetious' contains all vowels: True
Conclusion
Checking for all vowels in a string can be accomplished using LINQ methods like Where() and Distinct(), or with HashSet for better performance. The key is to filter the string for vowel characters and count the unique occurrences to determine if all five vowels are present.
