learningc/chapters/2/exercises/4.c

26 lines
650 B
C

#include <stdio.h>
void squeeze(char s1[], char s2[]);
int main() {
char sent[] = "I am a ssssnake!", banned[] = "as";
squeeze(sent,banned);
printf("Result: %s", sent);
return 0;
}
void squeeze(char s1[], char s2[]) {
int i,j,k,s; // i is writing position and k is reading position
for (i = 0, k = 0; s1[k] != '\0'; k++) {
for (s = 0, j = 0; s2[j] != '\0'; j++) {
// We check if we have a disallowed character and break if it is the case
if (s1[k] == s2[j]) {
s = 1;
break;
}
}
if (!s) {
s1[i++] = s1[k]; // As long as the character is not disallowed, write it and increase the write position
}
}
s1[i] = '\0';
}