I am trying to make a method that returns a string of words in the opposite order.
IE / "Rain in Spain falls mainly on" will return: "Spain mainly falls in the rain"
For this, I should not use any Java built-in classes, only basic Java.
So far, I:
lastSpace = stringIn.length();
for (int i = stringIn.length() - 1; i >= 0; i--){
chIn = stringIn.charAt(i);
if (chIn == ' '){
word = stringIn.substring(i + 1, lastSpace);
stringOut.concat(word);
lastS = i;
}
}
word = stringIn.substring(0,lastSpace);
stringOut.concat(word);
return stringOut;
My problem is that when returning stringOutit to the caller there will always be an empty string.
Am I doing something wrong? Perhaps my use string.concat()?
source
share