user4821907
user4821907

Reputation:

Rotate a given String layout clockwise

Given a String layout of the following form:

X......X
....X..X
....X..X

Rotate the above layout by 90 degrees clockwise which should be:

..X
...
...
...
XX.
...
...
XXX 

What's the easiest way to rotate the String characters clockwise by 90 degrees? The String layout can be of any form and any size. What if I have 100000x100000 size String layout?

public String rotate(String layout)

or

public void rotate(String layout)

Upvotes: 1

Views: 798

Answers (3)

J Atkin
J Atkin

Reputation: 3140

Edit: just saw that you wanted a String as the arg

You could use this:

public class SO {
    public static void main(String[] args) throws Exception {
        String string = "X......X\n" +
                        "....X..X\n" +
                        "....X..X\n";

        System.out.println(string);
        string = rotateClockwise(string);
        System.out.println(string);
    }

    static String rotateClockwise(String input) {
        String[] arr = input.split("\n");
        int length = arr[0].length();
        String[] ret = new String[length];
        for (int i = 0; i < ret.length; i++) {
            ret[i] = "";
        }

        for (int i = arr.length-1; i >= 0; i--) {
            char[] chars = arr[i].toCharArray();

            for (int j = 0; j < ret.length; j++) {
                ret[j] += chars[j];
            }
        }
        String output = "";
        for (String str: ret)
            output += str + "\n";

        return output;
    }
}

Please note this has NO error checking.

Upvotes: 0

CSCH
CSCH

Reputation: 651

Edit

I fixed the mistake as pointed out by the OP in the comments below. This should produce exactly what was required in the original question above.

 public static String rotateStringMatrixBy90(String matrix) {
    int numberOfRows = 3; // this I leave as an exercise
    int numberOfColumns = 8; // same with this one

    String newMatrix = "";

    int count = 0;
    String[] newMatrixColumns= matrix.split("\n");
    while (count < matrix.split("\n")[0].length()) {
        for (int i = newMatrixColumns.length - 1; i > -1; i--) {
            newMatrix = newMatrix + newMatrixColumns[i].charAt(count);
        }

        newMatrix = newMatrix + "\n";
        count++;
    }

    return newMatrix;
 }

And this is how you would use it:

    String m = "X......X\n" +
               "....X..X\n" +
               "....X..X";

    System.out.println(m);

    m = rotateStringMatrixBy90(m);
    System.out.println(m);

(Note: this assumes your using \n as the separator between the rows):

Upvotes: 1

kaykay
kaykay

Reputation: 556

public static String[] rotate(String[] originalArray) {
    String[] rotatedArray = new String[originalArray[0].length()];
    for (int i=0;i<rotatedArray.length;i++) {
        rotatedArray[i]="";
    }
    for (int j = 0; j < originalArray[0].length(); j++) {
        for (int i = originalArray.length - 1; i >= 0; i--) {
            rotatedArray[j] += originalArray[i].charAt(j);
        }
    }
    return rotatedArray;
}                

Upvotes: 0

Related Questions