题目1159: 单词替换
输入一个字符串,以回车结束(字符串长度<=100)。该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写。现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符串。
I love Tian Qin
I
You
You love Tian Qin
*** 提示已隐藏,点击上方 [+] 可显示 ***
2007年北京大学计算机研究生机试真题
/********************************** 日期:2013-4-5* 作者:SJF0115* 题号: 题目1159: 单词替换* 来源:http://acmclub.com/problem.php?id=1159* 结果:AC* 来源:天勤OJ* 总结:**********************************/#include<stdio.h>#include<stdlib.h>#include<string.h>int main (){int i,j,index,len,len2;char str[101],word[101],newword[101],temp[101];//freopen("C:\\Users\\SJF\\Desktop\\acm.txt","r",stdin); while(gets(str)){index = 0;len = strlen(str);scanf("%s",word);scanf("%s",newword);for(i = 0;i <= len;i++){if(str[i] == ' ' || i == len){temp[index] = '\0';if(strcmp(temp,word) == 0){printf("%s",newword);}else{printf("%s",temp);}if(str[i] == ' '){printf(" ");}else{printf("\n");}index = 0;memset(temp,0,sizeof(temp));}else{temp[index++] = str[i];}}getchar();}return 0;}