欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

字符串替换

程序员文章站 2022-07-14 07:58:54
...

字符串替换

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <string>
#include <stack>
#include <algorithm>
using namespace std;
string s[105],s1,s2;
char str[400];
stack <string> st;
int main()
{
    gets(str);
    cin >>s1;
    cin >>s2;
    int t=0;
    for(int i=0;i<strlen(str);i++)
    {
        if(str[i]!=' ')
        {
            s[t].push_back(str[i]);
        }
        else
        {
            t++;
        }
    }
    for(int i=0;i<=t;i++)
    {
        if(s[i]==s1)
            s[i]=s2;
    }
    for(int i=0;i<=t;i++)
    {
        if(!i)
            cout<<s[i];
        else
            cout <<' '<<s[i];
    }
    cout<<endl;
	return 0;
}