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";
}