-
Notifications
You must be signed in to change notification settings - Fork 2
/
2-ReplaceBlank.java
343 lines (300 loc) · 8.49 KB
/
2-ReplaceBlank.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package array;
/**
* @author WuPing
* @version 2016年4月8日 上午11:05:59
*/
public class ReplaceBlank {
public static void replaceBlank(char strings[], int length) {
// length为字符数组string的总容量
if (strings != null && length > 0) {
int originalLength = 0; // 字符数组实际长度
int numberOfBlank = 0; // 字符数组中的空格数
int i = 0;
while (strings[i] != '\0') {
++originalLength;
if (strings[i] == ' ')
++numberOfBlank;
++i;
}
// newLength为把空格替换成'%20'之后的长度
int newLength = originalLength + numberOfBlank * 2;
if (newLength > length)
return;
int indexOfOriginal = originalLength; // 字符数组起始扫描索引
int indexOfNew = newLength; // 替换空格后的字符数组起始扫描索引
while (indexOfOriginal >= 0 && indexOfNew > indexOfOriginal) {
// 从后向前扫描,遇到空格则替换成'%20',否则直接复制字符
if (strings[indexOfOriginal] == ' ') {
strings[indexOfNew--] = '0';
strings[indexOfNew--] = '2';
strings[indexOfNew--] = '%';
} else {
strings[indexOfNew--] = strings[indexOfOriginal];
}
--indexOfOriginal;
}
}
}
//空格在字符串中间
public static void Test1(int length) {
char[] test1Strings = new char[length];
test1Strings[0] = 'h';
test1Strings[1] = 'e';
test1Strings[2] = 'l';
test1Strings[3] = 'l';
test1Strings[4] = 'o';
test1Strings[5] = ' ';
test1Strings[6] = 'w';
test1Strings[7] = 'o';
test1Strings[8] = 'r';
test1Strings[9] = 'l';
test1Strings[10] = 'd';
test1Strings[11] = '\0'; // 模拟字符串结束符
System.out.println("Test1 begin: ");
System.out.print("替换空格前的字符数组:");
for (int i = 0; i < test1Strings.length; i++) {
if (test1Strings[i] == '\0') {
break;
}
System.out.print(test1Strings[i]);
}
System.out.println();
replaceBlank(test1Strings, length);
System.out.print("替换空格后的字符数组:");
for (int i = 0; i < test1Strings.length; i++) {
if (test1Strings[i] == '\0') {
break;
}
System.out.print(test1Strings[i]);
}
System.out.println();
}
//空格在字符串开头
public static void Test2(int length) {
char[] test2Strings = new char[length];
test2Strings[0] = ' ';
test2Strings[1] = 'h';
test2Strings[2] = 'e';
test2Strings[3] = 'l';
test2Strings[4] = 'l';
test2Strings[5] = 'o';
test2Strings[6] = 'w';
test2Strings[7] = 'o';
test2Strings[8] = 'r';
test2Strings[9] = 'l';
test2Strings[10] = 'd';
test2Strings[11] = '\0'; // 模拟字符串结束符
System.out.println("Test2 begin: ");
System.out.print("替换空格前的字符数组:");
for (int i = 0; i < test2Strings.length; i++) {
if (test2Strings[i] == '\0') {
break;
}
System.out.print(test2Strings[i]);
}
System.out.println();
replaceBlank(test2Strings, length);
System.out.print("替换空格后的字符数组:");
for (int i = 0; i < test2Strings.length; i++) {
if (test2Strings[i] == '\0') {
break;
}
System.out.print(test2Strings[i]);
}
System.out.println();
}
//空格在字符串末尾
public static void Test3(int length) {
char[] test3Strings = new char[length];
test3Strings[0] = 'h';
test3Strings[1] = 'e';
test3Strings[2] = 'l';
test3Strings[3] = 'l';
test3Strings[4] = 'o';
test3Strings[5] = 'w';
test3Strings[6] = 'o';
test3Strings[7] = 'r';
test3Strings[8] = 'l';
test3Strings[9] = 'd';
test3Strings[10] = ' ';
test3Strings[11] = '\0'; // 模拟字符串结束符
System.out.println("Test3 begin: ");
System.out.print("替换空格前的字符数组:");
for (int i = 0; i < test3Strings.length; i++) {
if (test3Strings[i] == '\0') {
break;
}
System.out.print(test3Strings[i]);
}
System.out.println();
replaceBlank(test3Strings, length);
System.out.print("替换空格后的字符数组:");
for (int i = 0; i < test3Strings.length; i++) {
if (test3Strings[i] == '\0') {
break;
}
System.out.print(test3Strings[i]);
}
System.out.println();
}
//字符串中有连续两个空格
public static void Test4(int length) {
char[] test4Strings = new char[length];
test4Strings[0] = 'h';
test4Strings[1] = 'e';
test4Strings[2] = 'l';
test4Strings[3] = 'l';
test4Strings[4] = 'o';
test4Strings[5] = ' ';
test4Strings[6] = ' ';
test4Strings[7] = 'w';
test4Strings[8] = 'o';
test4Strings[9] = 'r';
test4Strings[10] = 'l';
test4Strings[11] = 'd';
test4Strings[12] = '\0'; // 模拟字符串结束符
System.out.println("Test4 begin: ");
System.out.print("替换空格前的字符数组:");
for (int i = 0; i < test4Strings.length; i++) {
if (test4Strings[i] == '\0') {
break;
}
System.out.print(test4Strings[i]);
}
System.out.println();
replaceBlank(test4Strings, length);
System.out.print("替换空格后的字符数组:");
for (int i = 0; i < test4Strings.length; i++) {
if (test4Strings[i] == '\0') {
break;
}
System.out.print(test4Strings[i]);
}
System.out.println();
}
//字符数组为null
public static void Test5() {
System.out.print("Test5 begin: ");
replaceBlank(null, 0);
System.out.println("passed!");
}
//字符串为空串
public static void Test6(int length) {
char[] test6Strings = new char[length];
test6Strings[0] = '\0'; // 模拟字符串结束符
System.out.println("Test6 begin: ");
System.out.print("替换空格前的字符数组:");
for (int i = 0; i < test6Strings.length; i++) {
if (test6Strings[i] == '\0') {
break;
}
System.out.print(test6Strings[i]);
}
System.out.println();
replaceBlank(test6Strings, length);
System.out.print("替换空格后的字符数组:");
for (int i = 0; i < test6Strings.length; i++) {
if (test6Strings[i] == '\0') {
break;
}
System.out.print(test6Strings[i]);
}
System.out.println();
}
//字符串为一个空格
public static void Test7(int length) {
char[] test7Strings = new char[length];
test7Strings[0] = ' ';
test7Strings[1] = '\0'; // 模拟字符串结束符
System.out.println("Test7 begin: ");
System.out.print("替换空格前的字符数组:");
for (int i = 0; i < test7Strings.length; i++) {
if (test7Strings[i] == '\0') {
break;
}
System.out.print(test7Strings[i]);
}
System.out.println();
replaceBlank(test7Strings, length);
System.out.print("替换空格后的字符数组:");
for (int i = 0; i < test7Strings.length; i++) {
if (test7Strings[i] == '\0') {
break;
}
System.out.print(test7Strings[i]);
}
System.out.println();
}
//字符串中没有空格
public static void Test8(int length) {
char[] test8Strings = new char[length];
test8Strings[0] = 'h';
test8Strings[1] = 'e';
test8Strings[2] = 'l';
test8Strings[3] = 'l';
test8Strings[4] = 'o';
test8Strings[5] = 'w';
test8Strings[6] = 'o';
test8Strings[7] = 'r';
test8Strings[8] = 'l';
test8Strings[9] = 'd';
test8Strings[10] = '\0'; // 模拟字符串结束符
System.out.println("Test8 begin: ");
System.out.print("替换空格前的字符数组:");
for (int i = 0; i < test8Strings.length; i++) {
if (test8Strings[i] == '\0') {
break;
}
System.out.print(test8Strings[i]);
}
System.out.println();
replaceBlank(test8Strings, length);
System.out.print("替换空格后的字符数组:");
for (int i = 0; i < test8Strings.length; i++) {
if (test8Strings[i] == '\0') {
break;
}
System.out.print(test8Strings[i]);
}
System.out.println();
}
//字符串中全是空格
public static void Test9(int length) {
char[] test9Strings = new char[length];
test9Strings[0] = ' ';
test9Strings[1] = ' ';
test9Strings[2] = ' ';
test9Strings[3] = ' ';
test9Strings[4] = '\0'; // 模拟字符串结束符
System.out.println("Test9 begin: ");
System.out.print("替换空格前的字符数组:");
for (int i = 0; i < test9Strings.length; i++) {
if (test9Strings[i] == '\0') {
break;
}
System.out.print(test9Strings[i]);
}
System.out.println();
replaceBlank(test9Strings, length);
System.out.print("替换空格后的字符数组:");
for (int i = 0; i < test9Strings.length; i++) {
if (test9Strings[i] == '\0') {
break;
}
System.out.print(test9Strings[i]);
}
System.out.println();
}
public static void main(String[] args) {
int length = 100;
Test1(length);
Test2(length);
Test3(length);
Test4(length);
Test5();
Test6(length);
Test7(length);
Test8(length);
Test9(length);
}
}