package String;
/**
* 反向输出字符串
*/
import java.util.Scanner;
public class reversal {
public static void main(String[] args) {
System.out.println("Please enter the String:");
String str = new Scanner(System.in).nextLine(); // 输入字符串
toReverse1(str);
toReverse2(str);
}
public static void toReverse1(String str) {
StringBuffer s = new StringBuffer(str);
System.out.println(s.reverse().toString());
}
<pre name="code" class="java"> /**
* 字符串倒转
* 不运用StringBuffer
*/
public static void toReverse2() {
String s = "12345";
char[] chars = s.toCharArray();
int j = chars.length - 1;
for(int i = 0; i < j/2; i++) {
char c = chars[i];
chars[i] = chars[j - i];
chars[j - i] = c;
}
s = new String(chars);
System.out.println(s);
}
}