NOI:1946 单词替换

题目链接


题解:可以使用getchar()或者使用单词分割find()和substr()

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
int main(){
    string a[105];
    char t;
    t=getchar();
    int i=0;
    while(t!='\n'){
    while(t!=' '&&t!='\n'){
        a[i]+=t;
        t=getchar();
    }
        i++;
        if(t=='\n')break;
        t=getchar();
    }
    string b,c;
    cin>>b>>c;
    for(int j=0;j<i;j++){
        if(a[j]==b){
            a[j]=c;
        }
    }
    for(int j=0;j<i;j++){
        cout<<a[j];
        if(j!=i-1)cout<<" ";
    }
    cout<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wuzhenzi5193/article/details/80498676