提交后说答案是错误的,请告诉我它的逻辑错误是什么,而不是格式化或其他方法.
public class Solution {
public static String reverse(String s1){
int i=s1.length()-1;
String s2="";
while(i>=0){
s2=s2+s1.charAt(i--);
}
s2=s2+' ';
return s2;
}
// Return the reversed string. No need to print
public static String reverseEachWord(String s1) {
s1=s1+"";
String s2="";
int i=0;
char ch;
String temp="";
while(i<s1.length()){
ch=s1.charAt(i);
if(ch!=' '){
temp=temp+ch;
i++;
}
else{
temp=reverse(temp);
s2=s2+temp;
temp="";
i++;
}
}
return s2;
}
}
Run Code Online (Sandbox Code Playgroud)