input: "Who lives in a pineapple under the sea?" expected output: "Who sevil in a elppaenip rednu the sea?" (reverse size 5 and above words)
I think the if statement is the problem but I don't know why it's not working. I tried using the code from the if statement block inside the main method and it works fine.
public class Testing {
public static void main(String[] args) {
String sentence = "Who lives in a pineapple under the sea?";
System.out.println(spinWords(sentence));
}
public static String spinWords(String sentence) {
String[] words = sentence.split(" ");
int length = words.length;
String spinned = "";
for (int i = 0; i < length; i++) {
String word = words[i];
int wlength = word.length();
if (wlength > 4) {
String reversed = "";
for (i = wlength - 1; i >= 0; i--) {
reversed += "" + word.charAt(i);
}
spinned += reversed + " ";
} else {
spinned += word + " ";
}
}
spinned = spinned.trim();
return spinned;
}
}
This is my first stack overflow question btw and I don't really know what I'm doing. I would also love to see better implementations of this code ( for learning purposes ), thanks.