#include<stdio.h>
#include<string.h>
int main()
{
char a[1001];
while(gets(a)!=EOF)
{
for(int i = 0; i < strlen(a),a[i]!='\0'; ++i)
{
if(a[i] == 'y'&&a[i+1] == 'o'&&a[i+2] == 'u')
{
a[i] = 'w';
a[i + 1] = 'e';
for(int j = i + 2; j < strlen(a); ++j)
{
a[j] = a[j + 1];
}
}
}
printf("%s\n", a);
}
return 0;
}
最优解:
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s, s1, s2;
while(getline(cin,s))
{
int flag;
s1 = "you";
s2 = "we";
flag = s.find(s1,0);
while(flag != string::npos)
{
s.replace(flag, 3, s2);
flag = s.find(s1, flag + 1);
}
cout << s << endl;
}
return 0;
}