问题:给定一个字符串作为输入,将所有大写字母移动到字符串的末尾。例子:
move("Hello World")="ello orldHW"
问题是:我的代码并没有停止,ello orldHW
而是继续
ello orldHW // Expected output
ello orldWH // What I am actually getting
代码:
public class MoveUppercaseChars {
static String testcase1 = "Hello World";
public static void main(String args[]){
MoveUppercaseChars testInstance = new MoveUppercaseChars();
String result = testInstance.move(testcase1);
System.out.println("Result : "+result);
}
public String move(String str){
int len = str.length();
char ch;
for(int i=0; i<len; i++) {
ch = str.charAt(i);
if(((int)ch >= 65) && ((int)ch <= 90)) {
str = str.substring(0, i) + str.substring(i+1, len) + str.charAt(i);
}
}
return str;
}
}