题目地址:http://ac.jobdu.com/problem.php?pid=1510
-
题目描述:
-
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
-
输入:
-
每个输入文件仅包含一组测试样例。
对于每组测试案例,输入一行代表要处理的字符串。
-
输出:
-
对应每个测试案例,出经过处理后的字符串。
-
样例输入:
-
We Are Happy
-
样例输出:
-
We%20Are%20Happy
#include <stdio.h>
#include <string.h>
int main(void){
char str[1000001];
int len;
int new_len;
int i;
int cnt;
gets (str);
len = strlen (str);
cnt = 0;
for (i=0; i<len; ++i){
if (str[i] == ' ')
++cnt;
}
new_len = len + 2 * cnt;
str[new_len] = '\0';
--len;
--new_len;
while (len >= 0){
if (str[len] != ' '){
str[new_len] = str[len];
--new_len;
--len;
}
else{
str[new_len] = '0';
str[new_len - 1] = '2';
str[new_len - 2] = '%';
new_len -= 3;
--len;
}
}
puts (str);
return 0;
}