rotate string in java -


i trying rotate string 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();          (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 bad because it's not dynamic , create many stringbuilder line suggestions improvement please?

i not questioning approach probleme here. maybe should :d trying give advice on how advance now.

to make dynamically fit size of "string", have use array of stringbuilders:

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

to split line one-character array use:

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

i hope gets started.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -