C# Code - Vowels and Consonants Create an application with a method that accepts
ID: 3767194 • Letter: C
Question
C# Code - Vowels and Consonants
Create an application with a method that accepts a string as an argument and returns the number of vowels that the string contains. The application should have another method that accepts a string as an argument and returns the number of consonants that the string contains. The application should let the user enter a string, and should display the number of vowels and the number of consonants it contains.
Thanks to anyone who decides to help, I've been having trouble with this problem....
Explanation / Answer
private int Vowelcount(string word)
{
#region vowel counter
// string word = txtUserString.Text.ToLower();
char[] split = word.ToCharArray(); // split the word into a character array
char[] vowels = { 'a', 'e', 'i', 'o', 'u' }; // make an array for the vowels
int count = 0; // the count of the vowels; this will be returned
foreach (char vowel in split) // for each character in the array split..
{
if (vowels.Contains(vowel)) // if the array vowels contains a character that was in the word..
count++; // add 1 to count
}
#endregion
return (count);
}
private void consonantscount(string word)
{
#region consonant counter
string word = txtUserString.Text.ToLower();
char[] splitToCon = word1.ToCharArray(); // split the word into a character array
char[] consonants = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' }; // make an array for the consonants
int counter = 0; // the count of the consonants; this will be returned
foreach (char consonant in splitToCon) // for each character in the array split..
{
if (consonants.Contains(consonant)) // if the array consonants contains a character that was in the word..
counter++; // add 1 to counter
}
#endregion
return (counter);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.