Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have created a C# Windows Form App that allows the user to type in a word in a

ID: 3858999 • Letter: I

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 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();

}

}

}

Explanation / Answer

C# code:

using System;
class StringOperationVowel
{
public static void Main()
{
char[] sen = new char[100];

int i, vow = 0, conso = 0, special = 0, len;
Console.WriteLine("Enter the Length of the sentence ");
len = int.Parse(Console.ReadLine());
for (i = 0; i < len; i++)
{
sen[i] = Convert.ToChar(Console.Read());
}
for (i = 0; sen[i] != ''; i++)
{
if ((sen[i] == 'a' || sen[i] == 'e' || sen[i] ==
'i' || sen[i] == 'o' || sen[i] == 'u') ||
(sen[i] == 'A' || sen[i] == 'E' || sen[i] ==
'I' || sen[i] == 'O' || sen[i] == 'U'))
{
vow = vow + 1;
}
else
{
conso = conso + 1;
}
if (sen[i] == 't' || sen[i] == '' || sen[i] == ' ')
{
special = special + 1;
}
}

conso = conso - special;
Console.WriteLine("No. of vowels {0}", vow);
Console.WriteLine("No. of consonents {0}", conso);
Console.ReadLine();
Console.ReadLine();
}
}

output:

Enter the Length of the sentence
3
sun
No. of vowels 1
No. of consonants 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote