I don't know what to do next for the vowel counter

I am puzzled by why I am 25 when I type in my name for the program. He should count the vowels, and the last time I checked the chase, I have 2. Here is also an image of the destination .

/* Program : Ch5Ex12a - CountVowels * Programmer : Chase Mitchell * Date : 11/18/2015 * Description : User vowels are counted */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Ch5Ex12a { class Program { static void Main(string[] args) { int bacon=0; string Phrase; Console.WriteLine("Enter in letters"); Phrase = Console.ReadLine(); foreach (char a in Phrase) bacon += 1; foreach (char e in Phrase) bacon += 1; foreach (char i in Phrase) bacon += 1; foreach (char o in Phrase) bacon += 1; foreach (char u in Phrase) bacon += 1; Console.WriteLine(bacon); Console.ReadKey(); } } } 
+5
source share
5 answers
 foreach (char a in Phrase) bacon += 1; 

What do you think this does? This does not iterate over all the characters 'a' in Phrase . Instead, it iterates over all the characters in Phase and counts them. Its just that the name of the variable that each character in each iteration receives is called a . But this has nothing to do with the content.

You tried to do something like this:

 foreach (char c in Phrase) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') bacon += 1; } 

You should also check uppercase characters; either explicitly or by converting Phrase to lowercase. You can do this by going through Phrase.ToLower() instead of Phrase .

+9
source

Your problem in this code:

 foreach (char a in Phrase) bacon += 1; 

What does he do:

  • Iterates (jumps) the Phrase variable and produces each character after another.
  • It assigns the current value to a variable named a .
  • He adds one to the bacon . (Note: no verification!)

I think you think that all this came out of 'a' ... It is not.

There are several ways to overcome this. poke has already placed one. Another option would be this LINQ Count :

 char[] vowels = new char[] { 'a', 'e', 'i', 'o', 'u' }; int vowelCount = Phrase.Count(c => vowels.Contains(c)); 
+3
source

First of all, why is your variable called bacon ? You should call it something meaningful, like numVowels .

Secondly, the foreach does not work as you expect.

 foreach (char a in Phrase) bacon += 1; 

This does not say that "for each A in the phrase add one for the bacon." In fact, this means that each phrase (in this case, each character) stores it in a temporary variable (which you call a, e, i, o and u) and do something (in this case: add one to your counter ) So, the reason you get 25 is that you do 5 identical cycles, each of which adds one to the counter for each letter (5 x 5 = 25).

What you really want to do is a single foreach and compare each character to see if it is a vowel:

 foreach (char character in Phrase) { if (character == 'a' || character == 'e' || ... ) { numVowels += 1; } } 
+2
source

Install the vowelList array. If vowelList contains c, then increase the amount of bacon.

 String vowelList = "aeiou"; foreach (char c in Phrase) { if(vowelList.Contains(c)) bacon += 1; } 
0
source

Well, iterate over the string and count. To avoid clutter (and errors) in your code, try not to embed all the code in one Main method.

  // Model: what is/are vowel. // So if you're asked to count vowels in, say, Russian you know what to change private static HashSet<Char> s_Vowels = new HashSet<Char>() { 'a', 'e', 'i', 'o', 'u', // is 'w' considered being vowel? 'A', 'E', 'I', 'O', 'U', }; // Routine: count vowels in a given phrase // No input/output here, so it will work in WinForms, Command Line, ASP... private static int CountVowels(String phrase) { if (String.IsNullOrEmpty(phrase)) return 0; int result = 0; foreach (char letter in phrase) if (s_Vowels.Contains(letter)) result += 1; return result; } // Input/output static void Main(string[] args) { Console.WriteLine("Enter in letters"); string Phrase = Console.ReadLine(); Console.WriteLine(String.Format("It contains {0} vowels.", CountVowels(Phrase))); } 

Edit : your source code has a line using System.Linq; if you post it intentionally and you are allowed to use Linq, the procedure may be shortened:

  private static int CountVowels(String phrase) { if (String.IsNullOrEmpty(phrase)) return 0; else return phrase.Count(letter => s_Vowels.Contains(letter)); } 
0
source

Source: https://habr.com/ru/post/1236229/


All Articles