Question: C programming, if you already answer this, please skip, thanks fill in ... /* In this program, read stdin a line at a time, and
C programming, if you already answer this, please skip, thanks
fill in ...
/* In this program, read stdin a line at a time, and print the longest line. If that line is longer than 30 characters, print it in the format first ten characters...middle ten characters...last ten characters. Note: The longest line can be quite long. */
#include
char* readline() { char* result = NULL; int rsize; bool done = false; while (!done) { char current[BLOCKSIZE]; if (fgets(current, BLOCKSIZE, stdin) == NULL) { ... } else { int clen = strlen(current); if (result == NULL) { result = (char*) malloc(...); strcpy(result, current); } else { result = (char*) realloc(...) strcat(result, current); } ... } } return result; }
void printline(const char line[]) { if (line == NULL) return; int len = strlen(line); if (len < 30) { printf("%s ", line); } else { char left[11]; char middle [11]; char right[11]; strncpy(left, line, 10); left[10] = 0; strncpy(middle, line + len / 2 - 5, 10); ... printf("%s...%s...%s ", left, middle, right); } }
main() { char* longest = NULL; bool done = false; while (!done) { char* current = readline(); if (current == NULL) done = true; else if (longest == NULL || strlen(longest) < strlen(current)) { free(longest); longest = current; } } printline(longest); free(longest); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
