0

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;
    }

2 Answers2

0

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.

Shadowchaser
  • 586
  • 4
  • 8
  • thank you for your solution, it works for first user's character, but I think you missed the part in which we have the second character ( the Y) from the user and it should be moved in the right: Move all X characters to the beginning of the string and all Y characters to the end of the string. Can u help me with that too? I saw in c# negative index doesn't exist. –  Oct 14 '20 at 13:52
  • You just need to modify two lines to get it, but OK, I've added it now. – Shadowchaser Oct 14 '20 at 14:00
0

You can swap them in place, although it requires a lot of copying. Here's a function to move a character from one spot to another in an array. You can find several examples of this on the web and SO.

    public static void Move(char[] buffer, int from, int too)
    {
        if (from == too)
            return;

        char temp = buffer[from];

        if (too < from)
        {
            Array.Copy(buffer, too, buffer, too + 1, from - too);
        }
        else
        {
            Array.Copy(buffer, from + 1, buffer, from, too - from);
        }

        buffer[too] = temp;
    }

Then you just have to iterate through the array testing the characters, testing them and moving them to the right spot. Here's a function to do that:

    public static void ArrangeChars(char[] buffer, char left, char right)
    {
        int leftIndex = 0;
        int rightIndex = buffer.Length - 1;

        int i = 0;

        while(i <= rightIndex)
        {
            char ch = buffer[i];

            if (ch == left && i > leftIndex)
                Move(buffer, i, leftIndex++);
            else if (ch == right && i < rightIndex)
                Move(buffer, i, rightIndex--);
            else
                ++i;
        }
    }

Call it like so:

    static void Main(string[] _)
    {
        char[] data = "test me and move".ToCharArray();

        Console.WriteLine(new string(data));

        ArrangeChars(data, 'e', 't');

        Console.WriteLine("Move 'e' to start, move 't' to end");

        Console.WriteLine(new string(data));
    }

The usual caveats apply - this has not been well tested and if you use it, you should check all the edge cases.

MikeJ
  • 1,299
  • 7
  • 10