Question

In: Computer Science

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

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\n", 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\n", 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);
}

Solutions

Expert Solution

#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\n", 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\n", 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);
}


Related Solutions

using lunix or C programming to answer this lab please fill in the blanks with the...
using lunix or C programming to answer this lab please fill in the blanks with the answere being highlighted, so i can understand. First, type the following command:             sort employee What is the order that employee is sorted in? ___________________________________________ Give a brief description of how the file is sorted. _____________________________________________________________________________________________________________________________________________________________________________________________________ Now, sort on the field for last name.         sort +1 employee Look at the sorted file. Are all the names sorted in alphabetical order? ______________________ Give a brief description...
C++ Fill in the blanks please You are required to fill in the blanks in this...
C++ Fill in the blanks please You are required to fill in the blanks in this program. Consider that: -In main, the variables price and quantity were given the names of p (double) and q (int), respectively. -In the getData function, the parameters associated with the main variables p and q were called pp and pq, respectively. // Prototype: Do not include the names // of the variables in the prototype void getData (FILLTHEBLANK , FILLTHEBLANK); int main () {...
I want a different answer from people already answer. thanks What are the functions of a...
I want a different answer from people already answer. thanks What are the functions of a well-written code of ethics?
MANAGERIAL ACCOUNTING - TRUE OR FALSE STATEMENTS. (PLEASE SKIP IF YOU ARE NOT ABLE TO ANSWER...
MANAGERIAL ACCOUNTING - TRUE OR FALSE STATEMENTS. (PLEASE SKIP IF YOU ARE NOT ABLE TO ANSWER THEM ALL. THANK YOU!) If volume increases, all costs will increase. The relevant range of activity is the activity level where the firm will earn income. The high-low method is used in classifying a mixed cost into its variable and fixed elements. Contribution margin is the amount of revenues remaining after deducting cost of goods sold. Both variable and fixed costs are included in...
****Please answer them all, if you cannot, please skip**** Here are scores received by a class...
****Please answer them all, if you cannot, please skip**** Here are scores received by a class of Biology students on a 40 point exam. Each number is the score, out of 40 points, received by one student. The data are separated by gender. Scores, out of 40 points, received by female students: 17, 20, 30, 31, 31, 33.6, 34, 34.5, 35, 35, 36, 35.6, 36, 36, 36, 37, 37.5, 37, 38, 38 Scores, out of 40 points, received by male...
Please answer in C++ Thanks! Use an istream_iterator, the copy algorithm and a back_inserter to read...
Please answer in C++ Thanks! Use an istream_iterator, the copy algorithm and a back_inserter to read the contents of a text file that contains int values separated by whitespace. Place the int values into a vector of ints. The first argument to the copy algorithm should be the istream_iterator object that's associated with the text file's ifstream object. The second argument should be the istream_iterator object that's initialized using the class template istream_iterator's default constructor-- the resulting object can be...
please I want you to answer this question and most importantly, the answer is not already...
please I want you to answer this question and most importantly, the answer is not already available on this site or other site Course Learning Outcome: Apply Organizational behavior knowledge and skills to manage diversified culture in the organizational settings (Lo 2.2). Essay: Differences in Culture and Diversity at workplace Essay Write an essay about the differences in Culture and Diversity at workplace. Use examples, peer-reviewed journals to support your answer. This essay must be at least 1000-word in length....
Can you explain and answer part e and part f please? I already understand parts c...
Can you explain and answer part e and part f please? I already understand parts c and d Firm 1 and Firm 2 are functioning in a market as competitors. The inverse market demand for chicken is given by P (Y ) = 100 − 2Y , and the total cost function for any firm in the industry if given by TC(y) = 4y. (c) Suppose that two Cournot firms operated in the market and the reaction firm for Firm...
fill in the blanks please a) Already outstanding securities are traded in the ______________markets. b) An...
fill in the blanks please a) Already outstanding securities are traded in the ______________markets. b) An IPO is a ____________________ market transaction. c) Firms raise capital by selling newly issued securities in the ___________markets. d) New York Stock exchange is an example of a ____________________ market. e) The ___________ of the securities traded differentiates the money market from the capital market. f) The primary goal of a financial manager should be ___________________ g) Markets for short-term debt securities are called...
Please Provide the solution in java, already have a question which is answer in C++. Language:...
Please Provide the solution in java, already have a question which is answer in C++. Language: java. Please don't provide your email for private answer. Q1. Implement a program which allows the user to find the shortest path between two nodes in a graph possibly passing through a third node. I.e. the user should be able to ask questions like: Which is the shortest path from A to B passing through C? The program should output an ordered list of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT