Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming, if you already answer this, please skip, thanks fill in ... /* In

ID: 3797225 • Letter: C

Question

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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define BLOCKSIZE 100

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);
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define BLOCKSIZE 100

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);
}