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

/* test driver */

#include 
#include 
#include


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';
}

你可能感兴趣的:(C)