public class InvertedString {
public static String inverted(String str) {
if (str == null || str.length() == 0) {
return str;
}
int len = str.length();
char[] ch = str.toCharArray();
for (int i = 0; i < len/2; i++) {
char t = ch[i];
ch[i] = ch[len - 1 - i];
ch[len - 1 - i] = t;
}
return new String(ch);
}
public static void main(String[] args) {
System.out.println(inverted("hello world"));
System.out.println(inverted("你好"));
System.out.println(inverted("a"));
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/c33d5ff25098ee015b8157253e9a4dc3.png)