字符串插入(在指定位置后)

这一串文本中如果出现某个特定字符串 ,则在这个特定字符串之后插入字符串 hello;否则,将这串文本原样输出。
输入格式
第一行,有空格的字符串 s
第二行,查询的特定字符串 m
输出格式
一行处理后的字符串
输入数据
pen pencil penny
pen
输出数据
penhello penhellocil penhellony

把下面的程序改成符合要求的


#include <iostream>
#include <string>
using namespace std;
int main(){
    string s;
    int w;
    cin>>w;
    for(int i=1;i<=w;i++){
        cin>>s; 
        int len=s.size(); 
        cout<<s.insert(len-3,"hello")<<endl; 
    }
    return 0;
}
 

#include <iostream>
#include <string>
using namespace std;
int main() {
    string s, w;
    getline(cin, s);
    getline(cin, w);
    int i = 0, lw = w.size();

    while(s.find(w, i) < s.size()) {
        i = s.find(w, i);
        s.insert(i + lw, "hello");
        i += lw;
    }
    cout << s;
    return 0;
}

 觉得有用的话采纳一下哈