Reverse String (Đảo chuỗi)

Bài toán đảo chuổi, mình tưởng chỉ giành cho sinh viên mới ra trường chứ.

public String reverseStringByWords(String string) {
     final StringBuilder stringBuilder = new StringBuilder("");
     String[] words = string.split(" ");
     for (int j = words.length - 1; j >= 0; j--) {
          stringBuilder.append(words[j]).append(' ');
     }
     return stringBuilder.toString();
}
output: World Hello
String hi = "Hello World";
System.out.println(new StringBuilder(hi).reverse().toString());
output: dlroW olleH

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s