D-六级阅读_长沙学院2022暑假训练赛(一) (nowcoder.com)https://ac.nowcoder.com/acm/contest/38762/D链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
Bruce12138 was preparing for CET-6 recently. He found that the names of the protagonists in many articles were very long, so they were difficult to read and difficult to understand.
So he decided to change the names of all the protagonists in the article into his own names, so that it would be much more comfortable to read.
But his operation is a little slow. Please write a program to help him replace the name of the protagonist with his own name.
输入描述:
The first line contains a string T(1≤∣T∣≤10)T(1\le|T|\le10)T(1≤∣T∣≤10), indicates the name of the protagonist. Note that all characters in this line are uppercase. The second line contains a single integer n(1≤n≤50)n(1\le n\le50)n(1≤n≤50), indicates the number of sentences to be replaced. Then nnn lines follow, each line contains a string S(1≤∣S∣≤103)S(1\le|S|\le10^3)S(1≤∣S∣≤103) without spaces, indicates the sentences to be replaced. All characters except the protagonist's name are lowercase.
输出描述:
Output nnn lines, each line contains a string represents the sentence after replacing the name of the protagonist with "Bruce12138" (without quotes).
示例1
输入
复制ZHUYIN 1 ZHUYINhiahiahia
ZHUYIN 1 ZHUYINhiahiahia
输出
复制Bruce12138hiahiahia
Bruce12138hiahiahia
代码
#include<bits/stdc++.h>
using namespace std;
string m = "Bruce12138";
int main() {
string s,t;
int n;
cin >> s;
cin >> n;
while (n--) {
cin >> t;
int a = t.find(s);
while (a != -1) {
t = t.replace(a,s.size(), m);
a = t.find(s);
}
cout << t << endl;
}
return 0;
}
find 和replace的用法
find是为了找第一个符合的下标
replace替换
(82条消息) C++ replace() 函数用法_cai_niaocainiao的博客-CSDN博客_c++ replace