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?
CodePudding user response:
System.out.println((int) "?".charAt(0)); //63
System.out.println((int) ".".charAt(0)); //46
"?" and "." do not append to sbtemporary.
CodePudding user response:
Corrected 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);
}
if (b == 32 && sbtemporary.length() != 0){
for (int j = 1; j<= num-1; j ){
sb.append(" " sbtemporary);
}
sbtemporary.delete(0,sbtemporary.length());
}
sb.append(c);
}
String str = sb.toString();
return str;
}
Changes:-
- Removed the else if (b == 32) part where you are appending space to sbtemporary.
- Delete content of sbtemporary outside the j loop, else it will work only for num == 2.
- And in the if condition b == 32 added one more condition if sbtemporary is not empty.
CodePudding user response:
Add this checking, and add to sbtemporary for the repeated part.
System.out.println(repeatWords(str, 1));
if ( b == 63) {
sb = sb.append(" " sbtemporary);
sb.append(c);
break;
}
if (b == 63) {
for (int j = 1; j<= num; j ){
sb.append(" " sbtemporary);
}
sb.append(c);
break;
}
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 == 63) {
for (int j = 1; j<= num; j ){
sb.append(" " sbtemporary);
}
sb.append(c);
break;
}
if (b >= 65 && b <= 90 || b >= 97 && b <= 122) {
sbtemporary.append(c);
}
if (b == 32 && sbtemporary.length() != 0){
for (int j = 1; j<= num; j ){
sb.append(" " sbtemporary);
}
sbtemporary.delete(0,sbtemporary.length());
}
sb.append(c);
}
String str = sb.toString();
return str;
}
CodePudding user response:
You can simply do this instead,
String content = "How are you? I am fine.";
String[] words = content.split("\\s"); // `\\s` preserves any 2 or more repeated spaces
StringBuilder builder = new StringBuilder();
for(String word : words) {
for(int i = 0; i < n; i ) { // n is the no. of times to repeat the word
builder.append(word).append(" ");
}
}
System.out.printf("Repeated String : %s", builder.toString().trim());
A more tidy way with Java8, here Collections.nCopies(n, word)
simply returns you a list of the word repeated n
times, which is converted in a single String
separated by space, and eventually, all such repeated strings are converted in one finally resultant string separated by space.
String content = "How are you? I am fine.";
String[] words = content.split("\\s");
String result = Arrays.stream(words)
.map(word -> String.join(" ", Collections.nCopies(n, word)))
.collect(Collectors.joining(" ")); //n is the no. of times to repeat the word
System.out.printf("Repeated String : %s", result);