Reputation: 331
Can someone please look at my code? I need to count the characters from the entered textbox and have them display in the correct label box. (consonant, vowel, digit and other) I did have the digit one working and them messed it up working on the vowels.
// determines if the text is a consonant, vowel, digit or other character. Then it displays the count of each.
int consCount = 0;
int vowelCount = 0;
int digitCount = 0;
int otherCount = 0;
string inputString;
inputString = this.entryTextBox.Text.ToLower();
char[] vowels = new char[] {'a', 'e', 'i', 'o', 'u'};
string vow = new string(vowels);
for (int index = 0; index < inputString.Length; index++)
{
if (char.IsLetterOrDigit(inputString[index]))
{
if (inputString.Contains(vow))
vowelCount++;
}
else if (char.IsDigit(inputString[index]))
digitCount++;
}
this.voweldisplayLabel.Text = vowelCount.ToString();
this.digitsdisplayLabel.Text = digitCount.ToString();
this.constdisplayLabel.Text = consCount.ToString();
this.otherdisplayLabel.Text = otherCount.ToString();
Upvotes: 0
Views: 1850
Reputation: 331
I figured it out. I got both the vowels and digits to display properly. =) Now I am off to work on the consonants and other characters.
int vowelCount = 0;
int digitCount = 0;
string inputString;
inputString = this.entryTextBox.Text.ToLower();
char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };
string vow = new string(vowels);
for (int index = 0; index < inputString.Length; index++)
{
if (char.IsLetterOrDigit(inputString[index]))
{
if (vow.Contains(inputString[index]))
{
vowelCount++;
}
else if (char.IsDigit(inputString[index]))
digitCount++;
}
}
this.voweldisplayLabel.Text = vowelCount.ToString();
this.digitsdisplayLabel.Text = digitCount.ToString();
Upvotes: 0
Reputation: 11025
There are two problems in the code you posted:
The placement of the else
lines up with the for
and not the if
, as it should.
String.Contains
is trying to match up the entire char set...it is not picking it apart and looking for every character individually (it will only be true
when the string contains aeiou
as a chunk). You could use Linq to accomplish this as a one-liner if you'd like. Otherwise, nest a foreach
and loop across the char
list to find your match.
Upvotes: 1
Reputation: 1
I think you messed up with the else statement. It should be directly after the closing bracket of the if statement. But here it's behind the closing bracket of the for loop. Maybe you need some sleep ;)
Upvotes: 0