-1

I am trying to rotate a string by ninety degrees Exemple :

123 \n
456 \n
789 \n

shall become

741 \n
852 \n
963 \n

I did this

public String stringRotate(String string){

        String[] line= string.split("\n");
        StringBuilder builder = new StringBuilder();
        StringBuilder builder2 = new StringBuilder();
        StringBuilder builder3 = new StringBuilder();

        for (String s : line){

            builder.append(s.substring(0, 1));
            builder2.append(s.substring(1, 2));
            builder3.append(s.substring(2, 3));
        }

        builder.reverse().append("\n").append(builder2.reverse().append("\n").append(builder3.reverse().append("\n")));
        return builder.toString();

    }

    public static void main(String[] args) {
        Rotation r= new Rotation();
        String output = r.stringRotate("123" + "\n" + "456" + "\n" + "789");
        System.out.println(output);
    }

but it's really bad because it's not dynamic and i create as many stringbuilder as line Any suggestions for improvement please?

praxmon
  • 5,009
  • 22
  • 74
  • 121
ulquiorra
  • 931
  • 4
  • 19
  • 39

2 Answers2

2

I am not questioning your approach to this probleme here. Maybe someone should :D Just trying to give advice on how to advance from where you are now.

To make it dynamically fit the size of your "string", you have to use an array of StringBuilders:

StringBuilder[] builders = new StringBuilder[line.length];
for (int i = 0; i < line.length; i++) {
    builders[i] = new StringBuilder();
}

To split your line into a one-character array you could use:

for (String s : line){
    String[] chars = s.split(""); //it might be better to use charAt() instead.
    for (int i = 0; i < line.length; i++) {
        builders[i].append(chars[i]);
    }
}

I hope this gets you started.

SebastianH
  • 2,172
  • 1
  • 18
  • 29
  • thank you very much . I will start from your solutions . I almost got it . But the line: String[] chars = s.split(""); add a empty data in the array . Why please ? Thanks again – ulquiorra Mar 10 '14 at 13:15
  • solved , i use System.arraycopy() in order to remove the first empty element . Thank you very much man – ulquiorra Mar 10 '14 at 13:40
  • @user902509 Glad I could help. I am abusing String.split a bit here. As mentioned in my code comment above it would be better to access the individual characters with s.charAt(i). Actually the StringBuilder certainly has an `append` method that accepts characters as well. – SebastianH Mar 10 '14 at 14:12
0
import java.util.Scanner;


public class Rotate 
{
    public void rotate(char[][] original)
    {
        char[][] rotated=new char[3][3];
        for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            rotated[i][j]=original[j][i];

        }
        System.out.println("\n");
    }

    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            System.out.print(rotated[i][j]);

        }
        System.out.println("\n");
    }

}
public static void main(String[] args)
{
    char[][] original=new char[3][3];
    Scanner s=new Scanner(System.in);
    System.out.println("Enter values: ");
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            original[i][j]=s.next().charAt(0);

        }
    }

    Rotate r=new Rotate();
    r.rotate(original);
}

}

I think this would help you.You can rotate it 90 degrees. but the M * n values must be same.hope this helps you.