将字符串S1中任何与字符串S2中匹配的字符都删除,实现函数squeeze(s1,s2).

/* 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';
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值