Exercise 2-4

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

In order to delete each character from s2, we need to run the code for each element of s2. We achieve this by creating the index variable k that increments at the end of each iteration of a for-loop that runs until s2[k] is equal to \0. During each iteration, we compare the characters of s1 to s2[k] so that by the time the loop finishes, all characters of s2 are covered.

    /* squeeze:  delete all chars in s2 from s1 */
    void squeeze(char s1[], char s2[])
    {
        int i, j, k;
    
        for (k = 0; s2[k] != '\0'; k++) {
            for (i = j = 0; s1[i] != '\0'; i++)
                if (s1[i] != s2[k])
                    s1[j++] = s1[i];
            s1[j] = '\0';
        }
    }