[Ybtoj high-efficiency advanced 2.1] C. Word replacement] [string]

Insert picture description here

analysis

Because the whole sentence is not easy to read, consider inputting words one by one, and then read the spaces separately. As long as each word is judged whether it is the word to be replaced, it will be output as it is after replacing/not replacing.

Upload code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
string a[1001],s1,s2;
char c;
int main()
{
    
    
	int n=0;
	do
	{
    
    
		cin>>a[++n];
		scanf("%c",&c);
	}while(c==' ');
	cin>>s1;
	cin>>s2;
	for(int i=1;i<=n;i++)
	{
    
    
		if(a[i]==s1) cout<<s2<<' ';
		else cout<<a[i]<<' ';
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/dglyr/article/details/113349327