reverse1000.cpp: Write a program that reads up to 1000 lines from standard input
ID: 3563489 • Letter: R
Question
reverse1000.cpp: Write a program that reads up to 1000 lines from standard input and writes them in reverse order to standard output . By reverse I mean the last line read should be first to be printed. The first line read should be last to be printed. I do not mean reverse the letters in each line. "This is line1" --> "1enil si sihT" is NOT, NOT, NOT what I want. Also note that I am NOT talking about token oriented input here. A line here is a possibly empty sequence of characters ending in a newline character .
Explanation / Answer
#include #include #include void reverseLines(void); int main (void) { reverseLines(); return 0; } void reverseLines(void) { char *line; size_t len = 0; getline(&line, &len, stdin); if (strcmp(line, "") != 0) // NOT end of input { reverseLines(); printf ("%s", line); } free (line); }Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.