Reputation: 3
I am trying to create a program by the name of "Count Vowels" that accepts a phrase from a user and outputs the number of vowels within that phrase.
I used a while statement with a nested if statement that runs through all the vowels, and counts the vowels, so that it can be displayed. When ever I run the program it is not going through my while statement and checking the vowels as intended. Instead it just keeps adding one to my count variable after each iteration.
I am new to C# and tried to understand the issue logically, but am having trouble trying to get my program to execute. If someone could please help me understand what I'm doing wrong I would greatly appreciate it. Below is the code that I am using.
using System;
using static System.Console;
class CountVowels
{
static void Main()
{
string phrase;
int count=0;
int i=0;
Console.WriteLine("please enter a phrase of your choice");
phrase=Console.ReadLine();
while(i < phrase.Length)
{
if(phrase[i]=='a' ||
phrase[i]=='A' ||
phrase[i]=='e' ||
phrase[i]=='E' ||
phrase[i]=='i' ||
phrase[i]=='I' ||
phrase[i]=='o' ||
phrase[i]=='O' ||
phrase[i]=='u' ||
phrase[i]=='U')
{
count++;
}
{
i++;
}
Console.WriteLine("The number of vowels in your phrase is {0}",count);
Console.ReadLine();
}
}
}
Upvotes: 0
Views: 48
Reputation: 81493
Fixing your problem
var phrase = "asd";
var vowels = "aeiou";
int i = 0, count = 0;
while (i < phrase.Length)
{
if (vowels.Contains(char.ToLower(phrase[i])))
count++;
i++;
}
for loop
for (int i = 0; i < phrase.Length; i++)
if (vowels.Contains(char.ToLower(phrase[i])))
count++;
foreach
foreach (var c in phrase)
if (vowels.Contains(char.ToLower(c)))
count++;
foreach C#9 pattern matching
foreach (var c in phrase.ToLower())
if (c is 'a' or 'e' or 'o' or 'u')
count++;
Linq 1
count = phrase.Count(c => vowels.Contains(char.ToLower(c)));
Linq 2
count = phrase.ToLower().Count(c => vowels.Contains(c));
Regex
var count = Regex.Matches(phrase, "[aeiou]",RegexOptions.IgnoreCase).Count;
Efficient with a hashset
var hash = new HashSet<char>("aeiou".ToCharArray());
var count = phrase.ToLower().Count(x => hash.Contains(x));
Note this is not really an answer, it was more showing option.
Upvotes: 1
Reputation: 1043
A while loop is overkill for this but I am assuming you have a requirement for this? How about a little refactor to clean it up? You will need LINQ for this example.
using System.Linq;
string phrase;
int count=0;
int i=0;
Console.WriteLine("please enter a phrase of your choice");
phrase=Console.ReadLine();
while(i < phrase.Length)
{
string[] vowels = {"a", "e", "i", "o", "u"};
var characterValue = phrase[i].ToString().ToLower();
if(vowels.Contains(characterValue))
{
count++;
}
i++;
}
Console.WriteLine("The number of vowels in your phrase is {0}",count);
Console.ReadLine();
Or mush shorter still using LINQ but no loop:
using System.Linq;
Console.WriteLine("please enter a phrase of your choice");
var phrase=Console.ReadLine();
var count = phrase.Count(x => "aeiou".Contains(char.ToLower(x)));
Console.WriteLine("The number of vowels in your phrase is {0}",count);
Console.ReadLine();
Upvotes: 1