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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!