Exercise 2.4 - Compare S1, S2 To Delete Same Character in S1#

Question#

Write an alternative version of squeeze(s1,s2) that deletes each character in s1 that matches any character in the string s2.

/**
 * Exercise 2.4
 *
 * Let us write a version of squeeze(s1,s2) that deletes each
 * character in the string 1 that matches any character in the string s2
 *
 **/

#include<stdio.h>
#define MAXLINE 1000

int mgetline(char line[],int maxline);
void squeeze(char s1[],char s2[]);

int main(void)
{
    char s1[MAXLINE],s2[MAXLINE];
    
    putchar('s');
    putchar('1');
    mgetline(s1,MAXLINE);

    putchar('s');
    putchar('2');
    mgetline(s2,MAXLINE);

    squeeze(s1,s2);

    printf("%s",s1);

    return 0;
}

int mgetline(char s[],int lim)
{
    int i,c;
    
    for(i=0;i<lim-1 && (c=getchar())!=EOF && c != '\n';++i)
        s[i] = c;

    if(c == '\n')
        s[i++] = c;
    
    s[i] = '\0';
}

void squeeze(char s1[],char s2[])
{
    int i,j,k;
    k=0;

    for(i=0;s1[i]!='\0';++i)
    {
        for(j=0; (s1[i]!=s2[j]) && s2[j]!='\0' ;++j)
            ;
        if(s2[j]=='\0')
            s1[k++] = s1[i];
    }
    
    s1[k]='\0';
}

Explanation#

Let’s take the two inputs strings as:

s1: HelloWorld

s2: ol

Our desired output is:

HeWrd

This has removed the characters o and l from the first string. The way squeeze works is, it take each character from the first string and if there is no match found, stores it with a new index k. If there is a match found in s2, it simply skips it. The way it skips is realized by the following:

for(j=0; (s1[i]!=s2[j]) && s2[j]!='\0' ;++j)
    ;
if(s2[j]=='\0')
    s1[k++] = s1[i];

When the match is found s1[i] == s2[j] so our first for loop will end. The second if condtion will fail too as s2 is not iterated till the end, so we do not place the character in s1[k++] and we have successfully skipped it.