您的位置:首页 > 其它

hihocoder #1082 : 然而沼跃鱼早就看穿了一切

2016-08-26 15:20 357 查看
题目连接:http://hihocoder.com/problemset/problem/1082?sid=860611

此题利用了c++标准模板库的算法

s.find(string)
找到string 的位置

s.replace(pos,n,p)
将pos后的n个字符用p代替。

string::npos
是一个常数

#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;

int main() {
string str;
string t = "marshtomp";
string s = "fjxmlhx";
while (getline(cin, str, '\n'))
{
string lowstr = str;
for (int i = 0; i < str.length(); ++i)    //转换成小写
lowstr[i] = tolower(str[i]);
while (lowstr.find(t) != string::npos)   //查找是否存在t字符床
{
int pos = lowstr.find(t);    //查找指定的字符串,返回位置
str.replace(pos, t.size(), s);  //从位置pos开始的n个字符串替换成s
lowstr.replace(pos, t.size(), s);
}
cout << str << endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: