Of course, your immediate problem, as to why your cycle ends prematurely, is because you use
return within the cycle.
As specific documentation for
return statement ends the execution of the method in which it appears and returns control to the method that made the call.
Since your final sentence is
return words.ToString(); , I assume that your intention was to modify the
words arrangement in your cycle. But you do not do it anywhere, and I do not understand how you intended to achieve it.
Related to the previous point, the use of a
foreach is probably not appropriate if your intention was to modify each element of
words in your cycle. The use of a cycle
for would be better.
If you do not intend to change words with less than 5 letters, then you do not need a condition for that case.
words.ToString(); is not the correct way to put the words in the array
words . Rather, you should use
Example that works
Putting the above points into practice, I propose a simplified example that you can analyze to see how it works:
static void Main(string args)
Console.WriteLine(SpinWords("Buen día roñes Alex"));
private static string SpinWords(string sentence)
string words = sentence.Split();
for (int i = 0; i < words.Length; i++)
if (words[i].Length >= 5)
words[i] = new string(words[i].Reverse().ToArray());
return string.Join(" ", words);
Good morning Mr. Alex