剑指offer中题目:http://ac.jobdu.com/problem.php?pid=1510
-
题目描述:
-
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
-
输入:
-
每个输入文件仅包含一组测试样例。
对于每组测试案例,输入一行代表要处理的字符串。
-
输出:
-
对应每个测试案例,出经过处理后的字符串。
-
样例输入:
-
We Are Happy
-
样例输出:
-
We%20Are%20Happy
代码:
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string ReStr = "%20";
std::string InputStr;
getline(std::cin, InputStr);
size_t Pos = 0;
while (true)
{
Pos = InputStr.find(' ', Pos);
if (Pos == std::string::npos)
{
break;
}
std::string::iterator It = InputStr.begin() + Pos;
InputStr.replace(It, It+1, ReStr.begin(), ReStr.end());
Pos += ReStr.size();
}
std::cout << InputStr << std::endl;
//system("pause");
return 0;
}
/**************************************************************
Problem: 1510
User:
Language: C++
Result: Accepted
Time:870 ms
Memory:2284 kb
****************************************************************/