Search code examples
c#arraysstringcharswap

How to move chars in char array to the left and to the right in c# sharp


guys! I have this situation: Given a string and two more characters X and Y. Move all X characters to the beginning of the string and all Y characters to the end of the string. The order of the other characters in the string remains unchanged. I wrote two function MoveCharsLeft and MoveCharsRight to move X to the left and Y to the right, but there is an error System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' on this line of code char toReplace = splitText[charToCheck]; and I don't know how to handle it, in order to solve the exercise. Can, you guys, help me with that, how should be the functions?

static void Main()
    {
        string text = Console.ReadLine();
        char[] splitText = text.ToCharArray();
        string firstLetter = Console.ReadLine();
        char[] firstChar = firstLetter.ToCharArray();
        string secondLetter = Console.ReadLine();
        char[] secondChar = secondLetter.ToCharArray();
        char one = firstChar[0];
        char two = secondChar[0];
        Console.WriteLine(CheckChars(splitText, one, two));
        Console.ReadLine();
    }

    static char[] CheckChars(char[] splitText, char one, char two)
    {
        for (char letter = 'a'; letter <= 'z'; letter++)
        {
            if (Array.IndexOf(splitText, one) > -1)
            {
                MoveCharsLeft(splitText, one);
            }

            if (Array.IndexOf(splitText, two) > -1)
            {
                MoveCharsRight(splitText, two);
            }
        }

        return splitText;
    }

    static void MoveCharsLeft(char[] splitText, char charToCheck)
    {
        char toReplace = splitText[charToCheck];
        char currentLetter = splitText[0];
        for (int i = 0; i <= charToCheck; i++)
        {
            char temporary = splitText[i];
            splitText[i] = currentLetter;
            currentLetter = temporary;
        }

        splitText[0] = toReplace;
    }

    static void MoveCharsRight(char[] splitText, char charToCheck)
    {
        char toReplace = splitText[charToCheck];
        char currentLetter = splitText[-1];
        for (int i = 0; i <= charToCheck; i++)
        {
            char temporary = splitText[i];
            splitText[i] = currentLetter;
            currentLetter = temporary;
        }

        splitText[-1] = toReplace;
    }

Solution

  • You're not checking your boundaries. I would say CheckChars method is redundant and in MoveCharsLeft and MoveCharsRight you're not checking if you're still inside of splitText.

    Here's how I would implement MoveCharsLeft, I hope it'll guide you in the right direction.

    char[] MoveCharactersLeft(char[] text, char character) {
        var lowerCharacter = char.ToLower(character);
        var left = new List<char>();
        var right = new List<char>();
        
        foreach (var letter in text) {
            if (char.ToLower(letter) == lowerCharacter) {
                left.Add(letter);
                continue;
            }
            
            right.Add(letter);
        }
        
        left.AddRange(right);
        return left.ToArray();
    }
    

    And here's MoveCharactersRight:

    char[] MoveCharactersRight(char[] text, char character) {
        var lowerCharacter = char.ToLower(character);
        var left = new List<char>();
        var right = new List<char>();
        
        foreach (var letter in text) {
            if (char.ToLower(letter) == lowerCharacter) {
                right.Add(letter);
                continue;
            }
            
            left.Add(letter);
        }
        
        left.AddRange(right);
        return left.ToArray();
    }
    

    Now that you have both you simply call MoveCharsLeft and then the MoveCharactersRight on the result of MoveCharsLeft.