如何在不使用字符串函数的情况下编写一个java程序来反转字符串?
String a="Siva";
for(int i=0;i<=a.length()-1;i++)
{
System.out.print(a.charAt(i));
}
System.out.println("");
for(int i = a.length() - 1; i >= 0; --i)
{
System.out.print(a.charAt(i));
}这里的charAt()和a.length()是字符串函数
发布于 2013-12-05 14:58:05
这会有帮助的
public class StringReverse {
public static void main(String[] args){
String str = "Reverse";
StringBuilder sb = new StringBuilder(str);
str = sb.reverse().toString();
System.out.println("ReverseString : "+str);
}
}不能使用字符串方法
发布于 2013-12-05 15:00:11
String s = "abcdef";
char c[] = s.toCharArray();
for( int i = c.length -1; i>=0; i--)
System.out.print(c[i]);发布于 2013-12-05 15:02:06
使用StringBuilder类或StringBuffer类他们已经有了一个reverse()方法来反转字符串
StringBuilder str = new StringBuilder("india");
System.out.println("string = " + str);
// reverse characters of the StringBuilder and prints it
System.out.println("reverse = " + str.reverse());
// reverse is equivalent to the actual
str = new StringBuilder("malayalam");
System.out.println("string = " + str);
// reverse characters of the StringBuilder and prints it
System.out.println("reverse = " + str.reverse());http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
https://stackoverflow.com/questions/20393318
复制相似问题