import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
//reverseString(str);
//reverseString2(str);
//reverseString3(str);
reverseString4(str);
}
//方法一:栈-先进后出
public static void reverseString(String str){
Stack stack= new Stack();
for(char c: str.toCharArray()){
stack.push(c);
}
while(stack.size()!=0){
System.out.print(stack.pop());
}
}
//方法二:StringBuilder 的 reverse()方法
public static void reverseString2(String str){
StringBuilder sb = new StringBuilder(str);
System.out.print(sb.reverse().toString());
}
//方法三:由于String底层是字符数组,我们可以利用这一原理进行字符串反转
public static void reverseString3(String str){
char[] ch = str.toCharArray();
char temp = ' ';
for(int i=0,j=ch.length-1;i
temp = ch[i];
ch[i]=ch[j];
ch[j]=temp;
}
System.out.print(new String(ch));
}
//方法四:利用StringBuilder类的append()实现字符串反转
public static void reverseString4(String str){
StringBuilder sb = new StringBuilder();
for(int i=str.length()-1;i>=0;i--){
sb.append(str.charAt(i));
}
System.out.print(sb.toString());
}
}
发表于 2020-07-09 11:06:25
回复(1)