将字符串S1中任何与字符串S2中匹配的字符都删除,实现函数squeeze(s1,s2).
2016-11-23 21:41
429 查看
/* test driver */ #include <stdio.h> #include <string.h> #include<windows.h> void squeeze2(char s1[], char s2[]); //定义buffer的原因是想输出的能够看一下前后元素变化。 int main(void) { char *leftstr[] = //指针数组,它的一个数组元素为一个指针指向一个字符串数组的指针 { "", "a", "antidisestablishmentarianism", "beautifications", "characteristically", "deterministically", "electroencephalography", "familiarisation", "gastrointestinal", "heterogeneousness", "incomprehensibility", "justifications", "knowledgeable", "lexicographically", "microarchitectures", "nondeterministically", "organizationally", "phenomenologically", "quantifications", "representationally", "straightforwardness", "telecommunications", "uncontrollability", "vulnerabilities", "wholeheartedly", "xylophonically", /* if there is such a word :-) */ "youthfulness", "zoologically" }; char *rightstr[] = { "", "a", "the", "quick", "brown", "dog", "jumps", "over", "lazy", "fox", "get", "rid", "of", "windows", "and", "install", "linux" }; char buffer[32]; size_t numlefts = sizeof leftstr / sizeof leftstr[0]; size_t numrights = sizeof rightstr / sizeof rightstr[0]; size_t left = 0; size_t right = 0; for (left = 0; left < numlefts; left++) { for (right = 0; right < numrights; right++) { strcpy_s(buffer,100, leftstr[left]); squeeze2(buffer, rightstr[right]); printf("[%s] - [%s] = [%s]\n", leftstr[left], rightstr[right], buffer); } } system("pause"); return 0; } /* squeeze2: delete all characters occurring in s2 from string s1. */ //拷贝技巧:分别比较两个数组中的各个元素,定义三个变量,两个用来遍历两个数组,第三个用来下表引用,拷贝符合要求的内容。 void squeeze2(char s1[], char s2[]) { int i, j, k; int instr2 = 0; for (i = j = 0; s1[i] != '\0'; i++) { instr2 = 0; for (k = 0; s2[k] != '\0' && !instr2; k++) { if (s2[k] == s1[i]) { instr2 = 1; } } if (!instr2) { s1[j++] = s1[i]; } } s1[j] = '\0'; }
相关文章推荐
- 编写函数squeeze(s1,s2),将字符串s1中任何与字符串s2中字符匹配的字符都删除
- 2.4 重新编写函数squeeze(s1,s2),将字符串s1中任何与字符串s2中字符匹配的字符都删除。
- 练习2-4 重新编写函数squeeze(s1,s2),将字符串s1中任何与字符串s2中字符匹配的字符都删除。
- 练习 2-4 squeeze(s1, s2),将字符串 s1 中任何与字符串 s2 中字符匹配的字符 都删除
- 练习 2-4 squeeze(s1, s2),将字符串s1 中任何与字符串s2 中字符匹配的字符都删除。
- 将字符串s1中的任何与字符串s2中字符匹配的字符都删除
- 将字符串s1中任何与字符串s2中字符匹配的字符都删除
- 将字符串s1中任何与字符串s2匹配的字符都删掉
- PHP实现删除字符串中任何字符的函数
- 将字符串s1中任何与字符串s2匹配的字符都删去,存放于一个新的字符串s3中
- 将字符串s1中任何与字符串s2匹配的字符都删掉
- 写一个函数,实现两个字符串的比较。即实现strcmp函数,s1=s2时返回0,s1!=s2时返回二者第一个不同字符的ASCII值。
- PHP实现删除字符串中任何字符的函数
- 在S1字符串中删除S2字符串出现过的任意字符
- C++【String类】String删除单个字符,删除字符串的函数实现
- 在S1字符串中删除S2字符串出现过的任意字符
- 在S1字符串中删除S2字符串出现过的任意字符
- 删除字符串 s1 中与 s2 对应的字符的程序, 如何改?
- C++【String类】String删除单个字符,删除字符串的函数实现
- C语言strcspn()函数:检索字符串s1开头连续有几个字符都不含字符串s2中的字符