[英]Reverse word by word in a String
I am trying to reverse the words in a string word by word. 我正在尝试逐字反转字符串中的单词。 But I am running into a bit of trouble.
但是我遇到了麻烦。 I know many people have used
StringBuilder
to do this problem but I wanted to try it out without it. 我知道很多人都使用
StringBuilder
来解决这个问题,但是我想在没有它的情况下进行尝试。
Input: 输入:
Hi there
Output: 输出:
iH ereht
Currently, my input string stops at the last word. 目前,我的输入字符串停在最后一个单词。 I assume this is due to the fact that in my code, the reverse portion of the code only reverses when a
' '
or space is detected. 我认为这是由于以下事实:在我的代码中,代码的反向部分仅在检测到
' '
或空格时才反向。 I changed this by performing the reverse portion when the end of the string has been reached as well. 我也通过在到达字符串末尾时执行反向部分来更改此设置。 (
i == len
) However this does not seem to fix the problem. (
i == len
)但是,这似乎无法解决问题。 I assume there is some logic error inside my if
, else if
statements and for loop
as well. 我假设
if
, else if
语句和for loop
中也存在一些逻辑错误。 I was wondering if anyone could guide me in the right direction. 我想知道是否有人可以引导我朝正确的方向前进。
The test case string that I've been working on is 我一直在努力的测试用例字符串是
"Hi there Mr.Doge!"
The output I get right now is 我现在得到的输出是
iH ereht
<-- space at the end of the string. iH ereht
<-字符串末尾的空格。
I printed some text as the code proceeds and the last word ( Mr.Doge!
) is being stored into temp
but it is not being reverse. 随着代码的进行,我打印了一些文本,最后一个单词(
Mr.Doge!
)被存储到temp
但没有反向。
Here is the output when I compile the code: 这是我编译代码时的输出:
0
H
1
Hi
2
i
iH
3
t
4
th
5
the
6
ther
7
there
8
iH e
iH er
iH ere
iH ereh
iH ereht
9
M
10
Mr
11
Mr.
12
Mr.D
13
Mr.Do
14
Mr.Dog
15
Mr.Doge
16
Mr.Doge!
iH ereht
My code: 我的代码:
public static String reverseWord(String str){
int len = str.length();
String reverse = "", temp = "";
for (int i = 0; i < len; i++) {
System.out.println(i);
if (str.charAt(i) != ' '){
temp += str.charAt(i);
System.out.println(temp);
}
else if (str.charAt(i) == ' ' || i == len){
//if (str.charAt(i) == ' ') {
for (int j = temp.length() - 1; j >= 0; j--) { // reverse
reverse += temp.charAt(j); // append in reverse
System.out.println(reverse);
}
reverse += ' ';
temp = "";
}
}
return reverse;
}
With a couple of modifications this must work. 经过几处修改,该方法必须有效。 See comments in code, to see what I modified.
查看代码中的注释,以查看我所做的修改。
Code: 码:
public static void main(String[] args)
{
System.out.println(reverseWord("Hello world Liondancer"));
}
public static String reverseWord(String str)
{
int len = str.length();
String reverse = "", temp = "";
for (int i = 0; i < len; i++) { // i == len comparison is unuseful since 'i' won't never be 'len'
if (str.charAt(i) != ' ') {
temp = str.charAt(i) + temp; // What you did, but add the current character first, THIS IS THE REVERSE!!!
} else if (str.charAt(i) == ' ') {
reverse += temp + " ";
temp = "";
}
}
reverse += temp; // Added this outside the loop to add last word stored in 'temp'
return reverse;
}
Output: 输出:
olleH dlrow recnadnoiL
Note: 注意:
I deleted the nested for
since it is not necessary. 我删除了嵌套
for
,因为它是没有必要的。
try this change 试试这个变化
for (char c : str.toCharArray()) {
if (c != ' '){
temp = c + temp;
} else {
reverse += temp + ' ';
temp = "";
}
}
reverse += temp;
Reverse outside your loop and check that you're not at the last word (like so) - 在循环外倒转,并检查自己是否不是最后一个单词(像这样)-
public static String reverseWord(String str) {
int len = str.length();
String reverse = "", temp = " ";
for (int i = 0; i < len; i++) {
if (str.charAt(i) != ' ') {
temp += str.charAt(i);
} else if (str.charAt(i) == ' ' || i == len) {
if (i + 1 < len) {
for (int j = temp.length() - 1; j >= 0; j--) { // reverse
reverse += temp.charAt(j); // append in reverse
}
temp = " ";
} else {
temp = "";
}
}
}
for (int j = temp.length() - 1; j >= 0; j--) { // reverse
reverse += temp.charAt(j); // append in reverse
}
return reverse;
}
If i were to do it I would just store entire string in an arraylist. 如果我要这样做,我只会将整个字符串存储在arraylist中。 then say:
然后说:
for(i=0;i<len;i++)
temp.size()=len;
temp(length-i)=str(i);
print temp;
You can use the loop in this way too... 您也可以通过这种方式使用循环...
public String reverseString(String str){
String reverse="";
for(int i=str.length()-1; i>=0; i--){
reverse = reverse + str.charAt(i);
}
return reverse;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.