-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplaceString.java
More file actions
32 lines (27 loc) · 810 Bytes
/
ReplaceString.java
File metadata and controls
32 lines (27 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package string;
public class ReplaceString {
public static String replaceSpace(StringBuffer str) {
char[] array = str.toString().toCharArray();
str.delete(0,str.length());
int i =0;
while(i<=array.length-1){
while (i<array.length && array[i]!=' '){
str.append(array[i]);
i++;
}
int flag = 0;
while (i<array.length && array[i]==' '){
flag++;
i++;
}
for (int j = 0; j < flag; j++) {
str.append("%20");
}
}
return str.toString();
}
public static void main(String[] args) {
StringBuffer str = new StringBuffer("WeHappy");
System.out.println(replaceSpace(str));
}
}