小编vss*_*vss的帖子

我的代码在java中反转给定字符串的每个单词有什么问题?

提交后说答案是错误的,请告诉我它的逻辑错误是什么,而不是格式化或其他方法.

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)

java string reverse

-3
推荐指数
1
解决办法
79
查看次数

标签 统计

java ×1

reverse ×1

string ×1