如何倒写语句?

来源:百度知道 编辑:UC知道 时间:2024/06/14 05:52:51
一个hello world!语句,将其倒写过来,即!dlrow olleh,不许用数组。分析一下思路。我学到了《常用的类》这一章的容器部分,只用这章之前或者本章的内容
还没有学reverse(); 这个方法

public String daoshu(String str)
{
String temp = "";
for(int i=str.length()-1;i>=0;i--)
{
temp = temp + str.charAt(i);
}
return temp;
}

StringBuffer sb=new String("hello world!");
sb.reverse();

直接用reverse()方法,就能反转字符串

楼主打算用“算法”去实现反转?
那还不如用System.out.print("!dlrow olleh");
来的容易。。。这个你总学过吧?

很简单,用个for就可以:
String s="hello world!";
for(int i=s.length()-1;i>=0;i--)
System.out.print(s.charAt(i));