/* EXERCISE 2-4 */ #include <stdio.h> #define MAXLEN 20 /* A SMALL NUMBER FOR TESTING */ int loadstr(char s[]); void squeeze(char s1[], char s2[]); int c = EOF + 1; int error; enum boolean {NO, YES}; enum escapes {BELL = '\a', BACKSPACE = '\b', TAB = '\t', NEWLINE = '\n', VTAB = '\v', RETURN = '\r'}; main() { int len; char string_1[MAXLEN]; char string_2[MAXLEN]; /* LOAD TWO STRINGS FROM ONE INPUT LINE */ len = loadstr(string_1); if (error == NO) if (len == 0) printf("FIRST STRING MISSING\n"); else { len = loadstr(string_2); if (error == NO) if (len == 0) printf("SECOND STRING MISSING\n"); else { squeeze(string_1, string_2); printf("%s\n", string_1); } } printf("\nEND OF PROGRAM\n"); return 0; } /* LOADSTR: LOAD THE NEXT NON-BLANK STRING FROM THE INPUT LINE */ int loadstr(char s[]) { int i = 0; int end_string = NO; error = NO; if (c != '\n' && c != EOF) { while (error == NO && end_string == NO && (c = getchar()) != NEWLINE && c != EOF) if (c == ' ' || c == BELL || c == BACKSPACE || c == TAB || c == VTAB || c == RETURN) if (i > 0) end_string = YES; else ; else if (i < MAXLEN - 1) { s[i++] = c; } else { error = YES; printf("\nSTRING IS TOO LONG\n"); } s[i] = '\0'; } return i; } /* SQUEEZE: DELETE EACH CHAR IN s1 THAT MATCHES ANY CHAR IN s2. */ 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'; } }