Java递归练习-将字符串中的“x”全部移到这个字符串最后

来源:互联网 发布:武极天下神迹升级数据 编辑:程序博客网 时间:2024/04/29 00:16

Given a string, compute recursively a new string where all the lowercase 'x' chars have been moved to the end of the string. 

endX("xxre") → "rexx"
endX("xxhixx") → "hixxxx"
endX("xhixhix") → "hihixxx"

题目是codingbat上看到的自己做的答案

public String endX(String str) {    int index=str.indexOf("x");  int length=str.length();  String newX="";   if(index+1==length){ return str;}  else if(index==0&&(length>1)) {newX=str.substring(index+1,length) ;}  else if(index==-1) return str;  else{  newX=str.substring(0,index)+str.substring(index+1,str.length());}  return endX(newX)+"x";}


0 0