/** * resrever - a program that reverses its imput lines in a bit of a * more exterme fashion. (resrever = reverse(reverser)) */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // For printf and gets and such. #include // For strchr #include "string-utils.h" // For string_reverse // +-----------+------------------------------------------------------ // | Constants | // +-----------+ /** * The maximum number of characters per line. */ #define LINE_WIDTH 128 // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * The kernel. Recurse and print until you run out of lines. */ static void kernel () { char line[LINE_WIDTH+2]; // One line of text: line width + newline + \0 // LINE_WIDTH would also be acceptable. // Try to read a line. if (fgets (line, LINE_WIDTH+1, stdin) == NULL) return; // Reverse the remaining lines kernel (); // Drop the newline char *eol = strchr (line, '\n'); if (eol != NULL) *eol = '\0'; // Reverse the current line string_reverse (line); printf ("%s\n", line); } // kernel // +------+----------------------------------------------------------- // | Main | // +------+ int main () { // Do the work kernel (); // And we're done return 0; } // main