C# Program to Check if String contains Vowels-Learn how to find whether string contains a vowel or not with example program.
For example, if Input String is “Hello World”, Then output is “String Contains Vowel”.
For example if input string is “Sky”, Then output is ” No Vowel exists in the string”
This program convert the string into a char array with Upper case Letters.
Retrieve each char using a for loop and compare them with all vowels i.e. A,E,I,O,U.
If character is matching, we can print “String Contains Vowel” else print “No Vowel exists in the given string”
Example Program to Check if String contains Vowels
class Program
{
static void Main(string[] args)
{
String s = "SKY";
if (Program.IsVowel(s))
{
Console.WriteLine("String Contains Vowel");
}
else
{
Console.WriteLine("No Vowel exists in the given string");
}
}
public static bool IsVowel(String s)
{
s = s.ToUpper();
char[] Letters = s.ToCharArray();
for (int i = 0; i < Letters.Length; i++)
{
if (Letters[i] == 'A' || Letters[i] == 'E' || Letters[i] == 'I' || Letters[i] == 'O' || Letters[i] == 'U')
{
return true;
}
}
return false;
}
}
Input
Sky
Output
No Vowel exists in the given string