[Solved] how do you take a string then organize the words in it with how many times the word occurs in c#? [closed]


Use Linq GroupBy and Count:

string inputText = "hi my name is is";

var words = inputText.Split(' ').ToList();

var wordGroups = words.GroupBy(w => w).Select(grp => new {
                                                Word  = grp.Key,
                                                Count   = grp.Count()
                                            });

string outputText = string.Join("\n", wordGroups.Select(g => string.Format("{0}:\t{1}", g.Word, g.Count)));
/*
hi:  1
my:  1
name:  1
is:  2
*/

1

solved how do you take a string then organize the words in it with how many times the word occurs in c#? [closed]