将字符串s1中任何与字符串s2中字符匹配的字符都删除

/**************************************************************************

    类型:<c程序设计语言(第二版.新版)> 练习 2-4
名称:lx.c author: wubenzhimu data: 2012.11.16 功能:重新编写函数squeeze(s1,s2),将字符串s1中任何与字符串s2中字符匹配 的字符都删除。 **************************************************************************
*/ #include <stdio.h> #define MAXLINE 1000 /* 比较两个字符串,删除匹配的字符 */ void squeeze(char s1[], char s2[]); int main() { char s1[MAXLINE] = {'a','b','c','d','e','f','g','h','i','k',0}; char s2[MAXLINE] = {'a','c','e','g','i',0}; squeeze(s1, s2); printf("%s\n", s1); return 0; } void squeeze(char s1[], char s2[]) { int i, j, k; i = j = k = 0; while(s2[j] != '\0') { while(s1[i] != '\0') { if(s2[j] != s1[i]) s1[k++] = s1[i]; i++; } s1[k] = '\0'; i = k = 0; j++; } }

结果:

你可能感兴趣的:(字符串)