public class Reverse {
public static String ReverseStr(String str){
if(str==null&&str.length()<=1){
return str;
}else{
return new StringBuffer(str).reverse().toString();
}
}
public static String ReverseStr1(String str){
if ((null == str) || (str.length() <= 1)) {
return str;
}
return ReverseStr1(str.substring(1)) + str.charAt(0);
}
public static String ReverseStr2(String str){
if ((null == str) || (str.length() <= 1)) {
return str;
}else{
String temp = "";
char charArray[]=str.toCharArray();
for(int i=charArray.length-1;i>=0;i-- ){
temp += charArray[i];
}
return temp;
}
}
public static void main(String[] args) {
System.out.println(ReverseStr("oywl"));
System.out.println(ReverseStr1("oywl"));
System.out.println(ReverseStr2("oywl"));
}
}
http://codemonkeyism.com/java-interview-questions-write-a-string-reverser-and-use-recursion/
本文详细介绍了使用Java编程语言实现字符串反转的方法,包括使用`StringBuffer`类的`reverse()`方法和递归函数两种方式。通过具体代码示例展示了如何将输入字符串进行反转,并提供了一个简单的主函数来验证实现的有效性。
429

被折叠的 条评论
为什么被折叠?



