Question
I have created a C# Windows Form App that allows the user to type in a word in a textbox and it will count and
I have created a C# Windows Form App that allows the user to type in a word in a textbox and it will count and display how many consonants and vowels are in that word. I would like to change the app so that instead of reading a word from a textbox, I want it to read one or more words from a ListBox and count and display how many vowels and consonants are present.
Below is the C# code that I have currently. How can I change it so that it reads words from ListBox, not a TextBox.
private void button1_Click(object sender, EventArgs e)
{
string str = txtString.Text;
txtVowels.Text = CountVowels(str);
txtConsonants.Text = CountConsonants(str);
}
private string CountConsonants(string str)
{
int consonantsCount = 0;
foreach (char ch in str)
{
if (char.IsLetter(ch))
{
switch (char.ToUpper(ch))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
break;
default:
consonantsCount++;
break;
}
}
}
return consonantsCount.ToString();
}
private string CountVowels(string str)
{
int vowelsCount = 0;
foreach (char ch in str)
{
if (char.IsLetter(ch))
{
switch (char.ToUpper(ch))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowelsCount++;
break;
}
}
}
return vowelsCount.ToString();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started