Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Razvan Cirstea
2,493 PointsReturn the reversed words in a string, while preserving the word order
Hello,
I am stuck trying to solve the following exercise:
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example:
Input: "Let's take code contest" Output: "s'teL ekat edoc tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
This is the code that I wrote:
class Solution {
public String reverseWords(String s) {
String[] words = s.split(" ");
for(String word : words){
word = reverseWord(word);
}
return String.join(" ", words);
}
public String reverseWord(String s){
char[] letters = s.toCharArray();
s="";
for(int i=letters.length-1;i>=0;i--){
s=s + letters[i];
}
return s;
}
}
No matter what I do , it returns the same initial string. I checked and the reverseWord method is correctly returning the reversed words. I am almost sure that the issue comes from the for(String word:words) loop, somehow I have not set it up correctly .. Could someone please help me ? Please note that you need to return the string containing the reversed words in the initial word order.
2 Answers

Pedro Cabral
33,586 PointsI see a few problems all in your first method:
- In your first method you are returning words, which is just an array split, that's why you are getting the original output, because that's what you are returning. After your split, you can instantiate a new String to hold the reversed words with a =+. After each call to the second reversedWord method, you can add to the string an empty space " ". However this will also add a space after the last word of the sentence, so to make it even better I suggest you check if you are working on the last word, if not you add the space, if so, then you don't.
Your second method is fine.
Here is the working code:
public class Solution {
public static void main(String[] args) {
System.out.println(reverseWords("My name is Pedro"));
}
public static String reverseWords(String s) {
String[] words = s.split(" ");
String ss = "";
for(String word : words){
ss += reverseWord(word);
ss += " ";
}
return String.join(" ", ss);
}
public static String reverseWord(String s){
char[] letters = s.toCharArray();
s="";
for(int i=letters.length-1;i>=0;i--){
s=s + letters[i];
}
return s;
}
}
I also suggest you rename your methods better to signify what they are really doing. My variably ss is also very badly named.

Razvan Cirstea
2,493 PointsThank you, Pedro ! It worked !