/* For extra credit: What happens when the input contains NUL (byte 0)? Check your answer, by writing an auxiliary program that prints an input with NULs, and feeding that input to this program. Then modify this program so that it works properly with NULs. */ #include void print(char *s) { while (*s) putchar(*s++); } int length(char *s) { int n = 0; while (s[n]) ++n; return n; } void printline(char *s,int argc,char **argv) { int i; int vlen; int j; while (*s) { for (i = 1;i < argc;++i) { vlen = length(argv[i]); if (!vlen) continue; /* Can you figure out why this is necessary? */ for (j = 0;j < vlen;++j) if (s[j] != argv[i][j]) break; if (j == vlen) { print("***"); print(argv[i]); print("***"); s += j; break; } } if (i == argc) putchar(*s++); } putchar('\n'); } int main(int argc,char **argv) { char line[1000]; int linelen = 0; int ch; while ((ch = getchar()) != EOF) if (ch != '\n') line[linelen++] = ch; else { line[linelen++] = 0; printline(line,argc,argv); linelen = 0; } return 0; }