I want to repeat only the words in my input string. But the order of output is not right. Here's my code:
public static String repeatWords(String s, int num){
StringBuilder sb = new StringBuilder();
StringBuilder sbtemporary = new StringBuilder();
int leng = s.length();
for (int i=0; i < leng; i++){
char c = s.charAt(i);
int b = (int) c;
if (b >= 65 && b <= 90 || b >= 97 && b <= 122){
sbtemporary.append(c);
} else if (b == 32){
sbtemporary.append(" ");
}
if (b == 32){
for (int j = 1; j<= num-1; j++){
sb = sb.append(" " + sbtemporary);
sbtemporary.delete(0,sbtemporary.length());
}
}
sb.append(c);
}
String str = sb.toString();
return str;
}
s is the input string, num is the times that needs to repeat. The result I want is like :
When the input is : "How are you? I am fine."
The output should be like: "How How are are you you?"
But the result of my code is:
"How How are are you? you I I am am fine."
I don't really know where goes wrong, pls could someone help me with this?