zcmu--1614: Problem 3(字符串替换)

1614: Problem 3

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 98  Solved: 51
[Submit][Status][Web Board]

Description

fjxmlhx hates marshtomp, so he wants to ignore the "marshtomp" in every sentences. In order to making the sentence complete, he changes the word "marshtomp" into "fjxmlhx"

please create a class, then you must solve the problem via calling the method of the class with the object of the class.

Input

the input contains several lines

each line there exists a string whose length is no more than 200

Attention: the end of line has nothing to do with the beginning of the next line

Output

print the answer of the problem

Sample Input

marshTomp is beaten by fjxmlhx!

AmarshtompB

Sample Output

fjxmlhx is beaten by fjxmlhx!

AfjxmlhxB

【分析】字符串查找与替换函数(find() 与 replace())。注意不区分大小写

【代码】

#include<bits/stdc++.h>
using namespace std;

int main()
{
	string s;
	string s1="marshtomp",s2="fjxmlhx";
	while(getline(cin,s))
	{
		string ss=s;
		int len=s.length();
		for(int i=0;i<len;i++)s[i]=tolower(s[i]);
		while(s.find(s1)!=string::npos)
		{
			int pos=s.find(s1);
			s=s.replace(pos,9,s2);
			ss=ss.replace(pos,9,s2);
		}
		cout<<ss<<endl;		
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38735931/article/details/82660082