Remove characters from a C string which occur in a second string
This morning's 2AM project is K&R's Exercise 2-4. It takes two arguments, the first, a source string, and the second, a string of characters which should be removed from the first if present.
void squeeze(char str1[], const char str2[]) {
int i = 0;
int y = 0;
while (str1[i] != '\0') {
int j = 0;
int found = 0;
while (str2[j] != '\0') {
if (str1[i] == str2[j]) {
found = 1;
break;
}
++j;
}
if ( ! found) {
str1[y++] = str1[i];
}
++i;
}
// Trim off any excess string
str1[i - (i - y)] = '\0';
}
Comments
No comments yet.
Leave a Comment
Note: Your comment may require approval before it is posted to the site.