mardi 12 août 2014

Voyelle d'affichage les plus utilisé dans une chaîne c# - Stack Overflow


I count the vowels and the consonant in a string. Now I want to display the most used vowel and consonant in this string the code that I have for the counting


    private void Button_Click(object sender, RoutedEventArgs e)
{
char[] charArray = new char[] { 'a', 'e', 'i', 'o', 'u' };
string line = testBox.Text.ToLower();

char letter;
int vowels = 0;
int sug = 0;
for (int i = 0; i < line.Length; i++)
{
letter = line[i];

if (charArray.Contains(letter))
vowels++;
if (!charArray.Contains(letter))
sug++;
}

MessageBox.Show("number of vowels is" + vowels.ToString());
MessageBox.Show("number of vowels is" + sug.ToString());
}



Make the vowels and constants lists instead of an int counter then you can manipulate each list at a later stage.


    private void Button_Click(object sender, RoutedEventArgs e)
{
char[] charArray = new char[] { 'a', 'e', 'i', 'o', 'u' };
string line = testBox.Text.ToLower();

char letter;
List<char> vowels = new List<char>();
List<char> sug = new List<char>();
for (int i = 0; i < line.Length; i++)
{
letter = line[i];

if (charArray.Contains(letter))
vowels.Add(letter);
if (!charArray.Contains(letter))
sug.Add(letter);
}

MessageBox.Show("number of vowels is" + vowels.Count);
MessageBox.Show("number of vowels is" + sug.Count);

MessageBox.Show("most used vowel: " + vowels.GroupBy(x => x).OrderByDescending(xs => xs.Count()).Select(xs => xs.Key).First());
MessageBox.Show("most used constant: " + sug.GroupBy(x => x).OrderByDescending(xs => xs.Count()).Select(xs => xs.Key).First());
}



Ok here is one way to do it. It may be a little more advanced due to the heavy use of linq and lambadas. It does work, but I would recommend breaking some of the functionality out into functions.


char[] charArray = new char[] { 'a', 'e', 'i', 'o', 'u' };
string line = "bbcccaaaeeiiiioouu";

var vowelCounts = new Dictionary<char, int>();

foreach(var vowel in charArray)
{
vowelCounts.Add(vowel, line.Count(charInString => vowel == charInString));
}

var consonantCounts = new Dictionary<char, int>();

foreach(var consonant in line.Where(charInString => !charArray.Contains(charInString)).Distinct())
{
consonantCounts.Add(consonant, line.Count(charInString => consonant == charInString));
}

KeyValuePair<char, int> mostUsedVowel = vowelCounts.OrderBy(Entry => Entry.Value).FirstOrDefault();
KeyValuePair<char, int> mostUsedConsonant = consonantCounts.OrderBy(Entry => Entry.Value).FirstOrDefault();

string output1 = String.Format("The Vowel '{0}' was used {1} times", mostUsedVowel.Key, mostUsedVowel.Value);
string output2 = String.Format("The Consonant '{0}' was used {1} times", mostUsedConsonant.Key, mostUsedConsonant.Value);

MessageBox.Show(output1);
MessageBox.Show(output2);



As String is an enumerable of characters You can use LINQs GroupBy function to group by characters an then do all kinds of evaluation with the groups: http://dotnetfiddle.net/dmLkVb


var grouped = line.GroupBy(c=> c);

var vowels = grouped.Where(g => charArray.Contains(g.Key));
var mostUsed = vowels.OrderBy(v => v.Count()).Last();

Console.WriteLine("Distinct characters: {0}:", grouped.Count());
Console.WriteLine("Vowels: {0}:", vowels.Count());
Console.WriteLine("Most used vowel: {0} - {1}:", mostUsed.Key, mostUsed.Count());


I count the vowels and the consonant in a string. Now I want to display the most used vowel and consonant in this string the code that I have for the counting


    private void Button_Click(object sender, RoutedEventArgs e)
{
char[] charArray = new char[] { 'a', 'e', 'i', 'o', 'u' };
string line = testBox.Text.ToLower();

char letter;
int vowels = 0;
int sug = 0;
for (int i = 0; i < line.Length; i++)
{
letter = line[i];

if (charArray.Contains(letter))
vowels++;
if (!charArray.Contains(letter))
sug++;
}

MessageBox.Show("number of vowels is" + vowels.ToString());
MessageBox.Show("number of vowels is" + sug.ToString());
}


Make the vowels and constants lists instead of an int counter then you can manipulate each list at a later stage.


    private void Button_Click(object sender, RoutedEventArgs e)
{
char[] charArray = new char[] { 'a', 'e', 'i', 'o', 'u' };
string line = testBox.Text.ToLower();

char letter;
List<char> vowels = new List<char>();
List<char> sug = new List<char>();
for (int i = 0; i < line.Length; i++)
{
letter = line[i];

if (charArray.Contains(letter))
vowels.Add(letter);
if (!charArray.Contains(letter))
sug.Add(letter);
}

MessageBox.Show("number of vowels is" + vowels.Count);
MessageBox.Show("number of vowels is" + sug.Count);

MessageBox.Show("most used vowel: " + vowels.GroupBy(x => x).OrderByDescending(xs => xs.Count()).Select(xs => xs.Key).First());
MessageBox.Show("most used constant: " + sug.GroupBy(x => x).OrderByDescending(xs => xs.Count()).Select(xs => xs.Key).First());
}


Ok here is one way to do it. It may be a little more advanced due to the heavy use of linq and lambadas. It does work, but I would recommend breaking some of the functionality out into functions.


char[] charArray = new char[] { 'a', 'e', 'i', 'o', 'u' };
string line = "bbcccaaaeeiiiioouu";

var vowelCounts = new Dictionary<char, int>();

foreach(var vowel in charArray)
{
vowelCounts.Add(vowel, line.Count(charInString => vowel == charInString));
}

var consonantCounts = new Dictionary<char, int>();

foreach(var consonant in line.Where(charInString => !charArray.Contains(charInString)).Distinct())
{
consonantCounts.Add(consonant, line.Count(charInString => consonant == charInString));
}

KeyValuePair<char, int> mostUsedVowel = vowelCounts.OrderBy(Entry => Entry.Value).FirstOrDefault();
KeyValuePair<char, int> mostUsedConsonant = consonantCounts.OrderBy(Entry => Entry.Value).FirstOrDefault();

string output1 = String.Format("The Vowel '{0}' was used {1} times", mostUsedVowel.Key, mostUsedVowel.Value);
string output2 = String.Format("The Consonant '{0}' was used {1} times", mostUsedConsonant.Key, mostUsedConsonant.Value);

MessageBox.Show(output1);
MessageBox.Show(output2);


As String is an enumerable of characters You can use LINQs GroupBy function to group by characters an then do all kinds of evaluation with the groups: http://dotnetfiddle.net/dmLkVb


var grouped = line.GroupBy(c=> c);

var vowels = grouped.Where(g => charArray.Contains(g.Key));
var mostUsed = vowels.OrderBy(v => v.Count()).Last();

Console.WriteLine("Distinct characters: {0}:", grouped.Count());
Console.WriteLine("Vowels: {0}:", vowels.Count());
Console.WriteLine("Most used vowel: {0} - {1}:", mostUsed.Key, mostUsed.Count());

0 commentaires:

Enregistrer un commentaire