Does not complete the foreach cycle

1

I'm doing a method to invert words that contain 5 or more letters. The problem I have is that if my chain has more than 2 words, the cycle is exited. What am I doing wrong? (It's a Codewars exercise, I'm barely learning)

public static string SpinWords(string sentence)
{
    string[] words = sentence.Split();

    foreach (string word in words)
    {
        if (word.Length < 5)
        {
            char[] charArray = word.ToCharArray();
            return new string(charArray);
        }

        if (word.Length >= 5)
        {          
            char[] charArray = word.ToCharArray();
            Array.Reverse(charArray);
            return new string(charArray);
        }         
    }

    return words.ToString();
}
}
    
asked by AlexWong85 03.11.2016 в 23:40
source

3 answers

1

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 :

  

The return statement ends the execution of the method in which it appears and returns control to the method that made the call.

Other problems

  • 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 String.Join
  • Example that works

    Putting the above points into practice, I propose a simplified example that you can analyze to see how it works:

    using System;
    using System.Linq;
    
    // ...
    
    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);
    }
    

    Result

      

    Good morning Mr. Alex

        
    answered by 04.11.2016 / 01:52
    source
    2

    Your code does not complete the foreach because every time it enters an if (and always enters because if it does not meet the first one, it fulfills the second one) there is a return that what it does is to exit the method . I still do not understand what you want to do with that code, but if you ask is "because it does not complete the foreach", this is the answer.

        
    answered by 03.11.2016 в 23:44
    0

    modify your code trying not to use all the part of the reserved word return, I advise you always use that word (return) use at the end of your function.

    Now I see that your function comes out because you misuse the return

        
    answered by 03.11.2016 в 23:48