#include #include #define LINE_LENGTH 80 #define PAT_LENGTH 32 typedef enum {FALSE, TRUE} Bool_t; Bool_t IsSubstringOf(char pat[], char text[]) /* Returns TRUE iff pat is a substring of text. */ { int i = 0, j; while (text[i] != '\0') { j = 0; while (pat[j] != '\0' && text[i+j] != '\0' && pat[j] == text[i+j]) { ++j; } if (pat[j] == '\0') { return TRUE; } ++i; } return FALSE; } void PutLine(char line[]) /* Writes "line" to the output. */ { int i =0; while (line[i] != '\0') { putchar(line[i]); ++i; } putchar('\n'); } Bool_t GetLine(char line[], int length) /* Reads one line of the input into "line", truncating to "length" (including final '\0'). Returns TRUE iff EOF is encountered immediately. */ { int i = 0, c = getchar(); if (c == EOF) { return TRUE; } while (c != '\n' && c != EOF) { if (i < length - 1) { line[i] = c; ++i; } c = getchar(); } line[i] = '\0'; return FALSE; } void Grep (void) /* Reads first line of the input into "pat". Reads remainder of the input, line by line, copying each line to the output iff it contains "pat". */ { char line[LINE_LENGTH], pat[PAT_LENGTH]; GetLine(pat, PAT_LENGTH); while (!GetLine(line, LINE_LENGTH)) { if (IsSubstringOf(pat, line)) { PutLine(line); } } } int main (void) { Grep(); PutLine("\n\n *** Bye! ***"); return EXIT_SUCCESS; }