I have to return a new string with all the occurrences of input character shifted to the end. I tried to compare the given character by the string provided but not able to get the expected output.
public String moveChar(String str, char c) {
int len=str.length();
String newstr="";
int m=len-1;
for (int i=0; i<len; i++) {
char ch1=str.charAt(i);
char ch2=str.charAt(m);
if (ch1==c) {
newstr=newstr+ch2;
}
else {
newstr=newstr+ch1;
}
}
return newstr;
}
Expected Output: "heoll"
Actual Output: ""heooo"