what should I change in this, I know how to count all the vowels but not individually. Would like to state how many 'a', 'e', and etc. are there...thanks
string name;
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
int vow= 0;
Console.Write("Input characters:");
name= Console.ReadLine().ToLower();
for (int k=0; k < name.Length; k )
{
if (vowels.Contains(name[k]))
{
vow ;
}
}
Console.WriteLine("No. of vowels(s): {0}", vow);
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
Console.ReadLine();
CodePudding user response:
var vowels = new Dictionary<char, int> () {
{'a', 0}, {'e', 0}, {'i', 0}, {'o', 0}, {'u', 0}
};
Console.Write("Input characters:");
foreach (char c in Console.ReadLine().ToLower())
{
if (vowels.ContainsKey(c)) vowels[c] ;
}
CodePudding user response:
You want to use a Dictionary
of vowels, and every time you encounter a vowel increment a number associated with that value.
CodePudding user response:
You can use a Dictionary that keeps track of vowels and how many times they occured. Using Count
from LINQ, you can get the numbers for each occurences.
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u', ' ' };
Dictionary<char, int> count = new Dictionary<char, int>();
Console.Write("input: ");
string name = Console.ReadLine().ToLower();
foreach(char vowel in vowels)
{
count.Add(vowel, name.Count(x => x.Equals(vowel)));
}
foreach(var kvp in count)
{
Console.WriteLine($"vowel: {kvp.Key} ... occurences: {kvp.Value}");
}
// Prints
input: This is a line contianing most vowels
vowel: a ... occurences: 2
vowel: e ... occurences: 2
vowel: i ... occurences: 5
vowel: o ... occurences: 3
vowel: u ... occurences: 0
vowel: ... occurences: 6
CodePudding user response:
You can use .Count()
from System.Linq
to count number of vowels in a string
Returns the number of elements in a sequence.
using System.Linq;
...
//Do you consider space as a vowel?
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u', ' ' };
Console.Write("Input characters:");
var name= Console.ReadLine().ToLower().ToCharArray(); //Convert name to character array.
foreach(var vowel in vowels) //iterate over each vowel
{
var count = name.Count(x => x == vowel); //Count in entire name array.
Console.WriteLine($"Count of {vowel} = {count}");
}
If you want to use count of each vowel after foreach
loop, then store it in the Dictionary<char, int>
, otherwise Dictionary<char, int>
is not required. You can just print vowel and it's count inside foreach
loop
CodePudding user response:
Another suggestion using Dictionary
, but only storing the vowel Key
s that are actually found in name
:
var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
Console.Write("Input characters: ");
var name = Console.ReadLine().ToLower();
// Count the occurrence of any vowel in vowels that is present in name
var vowelAndCount = name
.Where(ch => vowels.Contains(ch))
.GroupBy(vowel => vowel)
.ToDictionary(vowel => vowel.Key, vowel => vowel.Count());
foreach (var vowel in vowels)
{
// If vowel was found in the previous operation, output the vowel count; otherwise, output 0
Console.WriteLine($"No. of '{vowel}'s: {(vowelAndCount.ContainsKey(vowel) ? vowelAndCount[vowel] : 0)}");
}