解题思路:采用最传统的思想即可,先分割单词,再查找替换单词。
注意事项:
参考代码:
struct Test{ char s[101]; }; void test(char *s,char *a1,char *a2,int n){ struct Test test[101]; int i,j,k=0; int num=0; //拆分单词 for(i=0;i<n;i++){ if(s[i]!=' '){ num++; } else{ for(j=0;j<num;j++){ test[k].s[j] = s[i-num+j]; } test[k].s[j] = 0; k++; num=0; } } if(num!=0){ for(j=0;j<num;j++){ test[k].s[j] = s[i-num+j]; } test[k].s[j] = 0; k++; num=0; } //单词比较 for(i=0;i<k;i++){ if(strcmp(test[i].s,a1)==0){ strcpy(test[i].s,a2); } } //输出结果 for(i=0;i<k;i++){ printf("%s ",test[i].s); } } int main(){ char s[101]; char a1[101]; char a2[101]; int n; gets(s); scanf("%s",a1); scanf("%s",a2); n = strlen(s); test(s,a1,a2,n); printf("\n"); system("pause"); return 0; }
0.0分
0 人评分