There is a line with random words written with a comma. Tell me how to determine the number of words with the same length.

  • And how are you trying to do the counting and what exactly is failing? - BlackWitcher
  • I just don’t quite understand the algorithm of the definition itself and how it is implemented exactly on the c # - Ares
  • you need to divide the line into words, and group them by length. After that, count the number in each group - Grundy
  • I divided the string into words and threw it into an array, but I don’t understand how to group them by length - Ares
  • 2
    Get an array with the number of words of each length in the string without registration and SMS s.Split(',').GroupBy(s => s.Length).Select(n => n.Count()).ToArray(); True, in real conditions, the result is not very useful, but now you have something to push off from - eastwing

2 answers 2

 int length = 5; //длина слова char[] splitsigns = { ',' }; List<string> words = "With,just,one,day,left,in,the,presidential,race,Hillary,Clinton,holds,a,four-point,lead,over,Donald,Trump,nationally".Split(splitsigns).ToList(); int amount = words.Count(w => w.Length == length); 
  • Ok, thanks, figured out - Ares
  • 2
    @Ares and minus what for? I help you, and you give me a minus, you are a good person! - Bulson
  • one
    Only in the question of grouping by length, and you count the number of words of one particular length. Something does not converge - Alexey Shimansky
  • @ Alexey Shimansky, I will allow myself to quote the question: "There is a line with random words written with a comma. Tell me how to determine the number of words with the same length." And I'm sorry I did not read what was written in the comments, because At this time, wrote the answer. - Bulson

Solution through LINQ already offered ...

Therefore, I will offer a solution in the old-fashioned way:

  Dictionary<int,int> resultDict=new Dictionary<int, int>();//Ключем словаря является длинна, а значением число повторений char[] splitsigns = { ',' }; List<string> words = "With,just,one,day,left,in,the,presidential,race,Hillary,Clinton,holds,a,four-point,lead,over,Donald,Trump,nationally".Split(splitsigns).ToList(); foreach (var word in words) { if (resultDict.ContainsKey(word.Length)) { resultDict[word.Length]++; } else { resultDict.Add(word.Length,1); } }