There is a line in which words are entered through the space, how to calculate the total number of letters of the smallest word, and the biggest one?

My code is:

if(txtString.Text.Length!=0) { str = txtString.Text.ToString(); } for (int i = 0; i <str.Length; i++) { if(str[i]==' ') { massstr[i] = a; b++; a = 0; } else { a++; } } for (int i = 0; i < b; i++) { if(max<massstr[i]) { max = massstr[i]; MessageBox.Show(""+max); } if(min>massstr[i]) { min = massstr[i]; MessageBox.Show("min " + min); } } txtResString.Text = "Суммарное количество букв самого маленького и большого слова равно " + (min + max) + " "; 
  • 2
    Well done! What is the question? - Igor
  • It seems that this task is given to everyone in a row :) I saw two similar ones for sure. - NewView
  • @Igor It is necessary to find the smallest word in the line read from the textbox, count the number of letters, the same with the biggest word, and add the number of the smallest letters to the largest one. My code does not work correctly. Thank you in advance! - Alexander
  • @Alex If your code does not work correctly, then there are debugging tools. In fact, you have two tasks: disassemble a line into words and count the number of letters in each word. - Gennady P
  • 2
    And breaking the string into an array and searching for min and max would be easier. And lines of code would be less. - Rootware

1 answer 1

The classic version, as taught in schools:

 public int SumOfShortestAndLenghtiestWord(string[] words) { if (words.Length == 0) throw new Exception("N/a to empty array."); var min = 0; var max = 0; bool isFirstWord = true; foreach (var word in words) { if (isFirstWord) { min = word.Length; max = word.Length; isFirstWord = false; continue; } if (word.Length > max) max = word.Length; if (word.Length < min) min = word.Length; } //min.Dump(); //max.Dump(); return min + max; } 

Suppose in this case min = 2, max = 11 sum - 13:

 var source = "Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua"; var result = SumOfShortestAndLenghtiestWord(source.Split(' ')); 

And this option is good because the detour is performed only once.

You can also write a variant based on linq:

 public int SumOfShortestAndLenghtiestWord(string[] words) { if (words.Length == 0) throw new Exception("N/a to empty array."); var min = words.Min(x => x.Length); var max = words.Max(x => x.Length); return min + max; } 

Compact, easy to read, you can shrink to the one-liner.

Update. Here in the comments they hint that the most canonical version is this:

 public int SumOfShortestAndLenghtiestWord(string[] words) { if (words.Length == 0) throw new Exception("N/a to empty array."); var min = words[0].Length; var max = words[0].Length; for(var i = 1; i < words.Length; i++) { if (words[i].Length > max) max = words[i].Length; if (words[i].Length < min) min = words[i].Length; } return min + max; } 
  • one
    Schools teach var max = words[0]; and then for (int i = 1; i < ... flag and the additional condition inside the loop are not needed :) - Andrey NOP