#include /* Exercise 2-4. Write an alternate version of squeeze (s 1,s2) that deletes each character in s1 that matches any character in the string s2. */ /* //squeeze: delete all c from s (needs below lines to work....) void squeeze(char s[], char c) { int i, j; for (i = j = 0; s[i] != '\0'; ++i) { if (s[i] != c) { s[j++] = s[i]; } s[j] = '\0'; } } //int i; //strip null chars that are just there in the middle of the string... otherwise printf does not work //for (i=0;i<5;++i) //{ //if (input[i] == '\0'){input[i] = input[i+1]; input[i+1] = '\0'; } //} */ //squeeze: Actual working reimplementation void squeeze(char s[], char c) { int i,j; for (i=0; s[i] != '\0'; ++i) { if (s[i] == c)//if s[i] is a charater to remove.... { j=i; while(s[j] != '\0'){s[j++] = s[j+1];}//shift all characters above left, overwriting the character --i;//deincrement i, since the string was just shrunk by one and the one just above could indeed be a matching char } } } //squeezy: Delete all chars in s0 that match each single char in s1 void squeezy(char s0[], char s1[]) { int i,j,z; for (i=0; s0[i] != '\0'; ++i) { for (z=0; s1[z] != '\0'; ++z) { if (s0[i] == s1[z])//if s0[i] is a charater to remove.... { j=i; while(s0[j] != '\0'){s0[j++] = s0[j+1];}//shift all characters above left, overwriting the character --i;//deincrement i, since the string was just shrunk by one and the one just above could indeed be a matching char } } } } int main(void) { //note: "char *" cannot be modified like; s[0] = 'a', char s[] needs to be used instead char input[] = "Test input"; //squeeze(input,'e'); squeezy(input,"t"); printf("%s\n", input); return 0; }