您的位置:首页 > 其它

字符串替换 hihoCoder1082 然而沼跃鱼早就看穿了一切

2015-08-13 22:41 309 查看
一直因为C++里面没有现成的替换函数而烦恼,只怪自己太懒懒的写

借这题写好这个函数,,以后直接用就好了

#include<map>
#include<set>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;

const int MX = 1e3 + 5;

string Replace(string s, string a, string b, bool Match = false) {
string tmp = s;
if(!Match) {
transform(tmp.begin(), tmp.end(), tmp.begin(), ::tolower);
transform(a.begin(), a.end(), a.begin(), ::tolower);
}

int pos;
while(true) {
if((pos = tmp.find(a)) != -1) {
tmp.replace(pos, a.length(), b);
s.replace(pos, a.length(), b);
} else break;
}
return s;
}

char S[MX], a[MX] = "marshtomp", b[MX] = "fjxmlhx";

int main() {
while(gets(S)) {
printf("%s\n", Replace(string(S), string(a), string(b)).c_str());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: