acm字符串排序c语言,字符串轮换 ACM

字符串替换 ACM

字符串替换

时间限制:3000 ms  |  内存限制:65535 KB

难度:2

描述

编写一个程序实现将字符串中的所有"you"替换成"we"

输入

输入包含多行数据

每行数据是一个字符串,长度不超过1000

数据以EOF结束

输出

对于输入的每一行,输出替换后的字符串

样例输入

you are what you do

样例输出

we are what we do

第一次写的代码:

#include

#include

char word[1000][1000];

char s[5]="you";

char c[5]="we";

int main(){

int x=0;

while(scanf("%s",word[x])!=EOF){

if(strcmp(word[x],s)==0)

strcpy(word[x],c);

x++;

}

for(int i=0;i

printf("%s",word[x]);

printf("\n");

}

想要结束。不知道怎么以EOF结束。貌似一直结束不了。

第二次

#include

#include

char s[1000];

char word[1000][1000];

char s1[5]="you";

char c[5]="we";

int main(){

while(gets(s)){

memset(word,0,sizeof(word));

int n;

n=strlen(s);

intk=0,j=0;

for(int i=0;i

if(s[i]!=' '){

word[k][j++]=s[i];

}

else{

j=0;

k++;

}

}

for(j=0;j<=k;j++){

if(strcmp(word[j],s1)==0)

strcpy(word[j],c);

}

for(j=0;j<=k;j++)

printf("%s ",word[j]);

printf("\n");

memset(s,0,sizeof(s));

}

}

感觉答题上没错误了。但还AC不过去。

分享到:

------解决方案--------------------

看到你申请的word的空间,发现很多都是浪费了,你要求输入1000个长度为1000以内的字符串吗?

输入EOF在linux下是输入ctrl +d.

------解决方案--------------------

仅供参考#include 

#include 

char s[256];

char *p;

int r,n,i;

int main() {

while (1) {

printf("请输入一行文字(空行结束),\"%%20\"将替换为\" \",\"你懂得\"将替换为\"XXXXXX\":\n");

fgets(s,256,stdin);

if ('\n'==s[0]) break;

p=s;

while (1) {

p=strstr(p,"%20");

if (p) {

memmove(p+1,p+3,strlen(p)-3+1);

p[0]=' ';

} else break;

}

p=s;

while (1) {

p=strstr(p,"你懂得");

if (p) {

memmove(p+6,p+6,strlen(p)-6+1);