Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I cannot get me code count the vowels in the entry text box. What have I done wrong?

C#
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();
Posted

Obviously, your bug is using inputString.Contains. It checks up is one string is a sub-string of another one, but your logic requires checking up is a single character is one of the vowels. Moreover, the calculation of the variable vow is completely pointless, and your call of Contains is just repeated inputString.Length times, giving the same result.

Instead, you should check up if you find inputString[index] in vowels. This is one of the simplest ways to do it:
http://msdn.microsoft.com/en-us/library/7eddebat.aspx[^].

For example:
C#
if (System.Array.IndexOf(vowels, inputString[index]) >= 0)
   ++vowelCount;

—SA
 
Share this answer
 
Comments
Member 10274514 14-Sep-13 21:12pm    
Thank you for the help and the link reference. =) Just learning. I know what I want to do, getting it done sometimes can be a battle. I'm off to read.
Sergey Alexandrovich Kryukov 14-Sep-13 21:43pm    
Sure. Please don't forget to accept the answer formally (green button).
In all cases, your follow-up questions, if any, will be welcome.
—SA
You can use this:
..............................

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int i, conso = 0, count = 0;
String letter;
letter=Console.ReadLine();
for (i = 0; i < letter.Length; i++)
{

if (letter[i] == 'a' || letter[i] == 'e' || letter[i] == 'i' || letter[i] == 'o' || letter[i] == 'u' || letter[i] == 'A' || letter[i] == 'E' || letter[i] == 'I' || letter[i] == 'O' || letter[i] == 'U')
{
++count;
Console.WriteLine(letter[i]+" is Vowel");
}
else
{
++conso;
Console.WriteLine(letter[i]+" is Consonant ");
}

}
Console.WriteLine("Total Number of vowels are : " + conso);
Console.WriteLine("Total Number of vowels are : " + count);
}
}
}
 
Share this answer
 
C#
string inputString;
inputString = this.entryTextBox.Text.ToLower();

char[] vowels = {'a', 'e', 'i', 'o', 'u'};
string vow;
int vowelCount = 0;
int digitCount = 0;


for int j = 0; j < 6; j++)
{
    vow = vow + vowels[j];
}

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


So there you are, your code, refurbished ... or something like that ...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Sep-13 22:01pm    
Not only it won't work, the code is much worse than the OP's, just look at the string concatenation repeated in the loop; do you even know why it's wrong? Do you know that strings are immutable? You just repeated wrong OP's solution in less elegant and efficient way. Why? Have some shame. You should at least test your solutions before posting.
—SA
The_Inventor 15-Sep-13 5:17am    
They aren't numbers so maybe j < 24 ?
Sergey Alexandrovich Kryukov 15-Sep-13 11:38am    
I don't understand what you are talking about. The whole idea to use Contains is wrong, why isn't it obvious?
You and OP find a sub-string of a string, but checking up for a vowel means finding a character in character array. See the difference?
All you added was ineffective calculation (OP's code was much better) of a string from an array, but this string is not even needed.
—SA
The_Inventor 16-Sep-13 0:25am    
His 'question' was: "I cannot get me code count the vowels in the entry text box. What have I done wrong?"

There are probably a half a dozen different ways to do the scan and compare routine that are better that what he presented. I am still confused as how a 'this pointer' can be referenced with a '.' instead of a '->', and how a predefine type like 'char' is being used as a declared variable.
Sergey Alexandrovich Kryukov 16-Sep-13 1:19am    
You are confused much more than you think. In any case, you should not have answered. You still don't understand the simple thing I tried to explain. And your last comment shows that you have no clue on .NET programming. How come you decided to write anything on this topic? Are you serious? You really don't know how "this" and '.' work (there is no '->')? You probably also have no idea how value and reference types work. Honestly, have some shame. You need to learn the very basics.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900