VJ_单词替换_stringstream

输入一个字符串,以回车结束(字符串长度<=100)。该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写。现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符串。

Input

输入包括3行,
第1行是包含多个单词的字符串 s;
第2行是待替换的单词a(长度 <= 100);
第3行是a将被替换的单词b(长度 <= 100).

s, a, b 最前面和最后面都没有空格.

Output

输出只有 1 行,将s中所有单词a替换成b之后的字符串。

Sample

Inputcopy Outputcopy
You want someone to help you
You
I
I want someone to help you

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

int main()
{
    string s,a,b;
    int f;

    while( getline( cin,s ) )
    {
        cin>>a>>b; 
        f=0;

        stringstream ss; ss.clear(); ss.str("");
        ss<<s; 
        while( ss>>s )
        {
            if( f ) cout<<" ";
            else    f=1;
            if( s==a ) 	cout<<b;
            else		cout<<s;    //
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_63173957/article/details/125136090
vj
今日推荐