Page 48
Write an alternate version of squeeze(s1,s2)
that deletes each character in the
string s1
that matches any character in the string s2
.
#include <stdio.h>
#include <stdbool.h>
void squeeze(char s1[], char s2[])
{
int i, j, k;
int num_unique;
bool found;
/* Reduce s2 to unique characters */
for (i = num_unique = 0; s2[i] != '\0'; i++) {
found = false;
for (j = 0; j < num_unique; j++) {
if (s2[j] == s2[i]) {
found = true;
break;
}
}
if (!found) {
s2[num_unique++] = s2[i];
}
}
s2[num_unique] = '\0';
/* Eliminate from s1 all found in s2 */
for (i = j = 0; s1[i] != '\0'; i++) {
found = false;
for (k = 0; k < num_unique; k++) {
if (s1[i] == s2[k]) {
found = true;
break;
}
}
if (!found) {
s1[j++] = s1[i];
}
}
s1[j] = '\0';
}
int main()
{
char s1[] = "abcdefg";
char s2[] = "aceg";
squeeze(s1, s2);
printf("%s", s1);
}