字符串替换
-
描述
-
编写一个程序实现将字符串中的所有"you"替换成"we"
-
输入
-
输入包含多行数据
每行数据是一个字符串,长度不超过1000
数据以EOF结束
输出
- 对于输入的每一行,输出替换后的字符串 样例输入
-
you are what you do
样例输出
-
we are what we do
来源
两张方法(仅供参考):
#include <stdio.h>
#include <string.h>
int main()
{
char a[1001],c;
while(scanf("%s%c",a,&c)!=EOF)
{
if(strstr(a,"you")!=NULL)
{ //判断字符串中是否有“you”
int n=strlen(a);
for(int i=0;i<n;i++)
{ //将you替换成we
if(a[i]=='y'&&a[i+1]=='o'&&a[i+2]=='u')
{
printf("we");i+=2;
}
else printf("%c",a[i]);
}
}
else
printf("%s",a); //没有直接输出
if(c=='\n')
printf("\n"); //关于空格和换行
else
printf(" ");
}
return 0;
}*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char a[1000];
int i,j,n;
while(gets(a))
{
n=strlen(a);
for(i=0;i<n;i++)
if(a[i]=='y'&&a[i+1]=='o'&&a[i+2]=='u')
{
a[i]='w';
a[i+1]='e';
for(j=i+2;j<n;j++)
a[j]=a[j+1];
}
printf("%s\n",a);
}
return 0;
}
-
输入包含多行数据