Reputation: 16479
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.
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 assume there is some logic error inside my if
, else if
statements and for loop
as well. 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.
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.
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;
}
Upvotes: 1
Views: 528
Reputation: 1
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;
}
Upvotes: 0
Reputation: 201527
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;
}
Upvotes: 0
Reputation: 136122
try this change
for (char c : str.toCharArray()) {
if (c != ' '){
temp = c + temp;
} else {
reverse += temp + ' ';
temp = "";
}
}
reverse += temp;
Upvotes: 1
Reputation: 848
If i were to do it I would just store entire string in an arraylist. then say:
for(i=0;i<len;i++)
temp.size()=len;
temp(length-i)=str(i);
print temp;
Upvotes: 0
Reputation: 34176
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.
Upvotes: 3