简单字符串处理,从规定位置pos开始查找,string::find(char *, pos);还要注意replace的用法string::replace(pos, length, char*);
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; string replacement[7][2] = { { "%", "%25" }, { " ", "%20" }, { "!", "%21" }, { "$", "%24" }, { "(", "%28" }, { ")", "%29" }, { "*", "%2a" } }; int main() { //freopen("D:\\t.txt", "r", stdin); string st; while (getline(cin, st) && st != "#") { for (int i = 0; i < 7; i++) { int pos = st.find(replacement[i][0]); while (pos != string::npos) { st.replace(pos, 1, replacement[i][1]); pos += 2; pos = st.find(replacement[i][0], pos); } } cout << st << endl; } return 0; }