Home >Backend Development >C#.Net Tutorial >C# program to count vowels in string
You need to check for vowels and consonants, but don't forget to check for upper and lower case.
To count vowels, check the "aeiou" characters individually, i.e.
if (myStr[i] == 'a' || myStr[i] == 'e' || myStr[i] == 'i' || myStr[i] == 'o' || myStr[i] == 'u' || myStr[i] == 'A' || myStr[i] == 'E' || myStr[i] == 'I' || myStr[i] == 'O' || myStr[i] == 'U') { vowel_count++; }
Here is the code to count the number of vowels in a string.
Live Demo
using System; public class Demo { public static void Main() { string myStr; int i, len, vowel_count, cons_count; myStr = "Avengers"; vowel_count = 0; cons_count = 0; // find length len = myStr.Length; for(i=0; i<len; i++) { if(myStr[i] =='a' || myStr[i]=='e' || myStr[i]=='i' || myStr[i]=='o' || myStr[i]=='u' || myStr[i]=='A' || myStr[i]=='E' || myStr[i]=='I' || myStr[i]=='O' || myStr[i]=='U') { vowel_count++; } else { cons_count++; } } Console.Write("Vowels in the string: {0}", vowel_count); } }
Vowels in the string: 3
The above is the detailed content of C# program to count vowels in string. For more information, please follow other related articles on the PHP Chinese website!