This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertString.java
56 lines (48 loc) · 1.52 KB
/
InsertString.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.ematrix;
public class InsertString {
public String transformString(char[] S){
StringBuilder result=new StringBuilder();
for(int i =0;i<S.length;i++){
if(S[i] == ' ')
{
result.append("%20");
continue;
}
result.append(S[i]);
}
return result.toString();
}
public void transformStringTwo(char[] str,int truelength){
int spaceCount = 0;
int index;
int i = 0;
for(i =0;i<truelength;i++){
if(str[i] == ' ')
{
spaceCount++;
continue;
}
}
index = truelength + spaceCount * 2;
if (truelength < str.length) str[truelength]='\0';
for (i = truelength - 1; i >= 0; i-- ) {
if (str[i] == ' ') {
str[index-1] ='0';
str[index - 2] = '2';
str[index - 3] = '%';
index = index - 3;
} else {
str[index - 1] = str[i];
index--;
}
}
}
public static void main(String[] args){
char[] str = "Mr John Smith ".toCharArray();
InsertString insertString =new InsertString ();
String result=insertString.transformString ("Mr John Smith".toCharArray());
insertString.transformStringTwo(str,13);
for (int i = 0; i < str.length; i++)
System.out.print(str[i]);
}
}