C Programming Missing Code In this program read stdin a
/* C Programming: Missing Code - - -
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)
{
- - - // done = true; (or) exit (0) (or) something else?
}
else
{
int clen = strlen(current);
if (result == NULL)
{
result = (char*) malloc(- - -); // clen?
strcpy(result, current);
}
else
{
result = (char*) realloc(- - -); // result, strlen(result) + clen ?
strcat(result, current);
}
- - - // What ????
}
}
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);
- - - // could be --> middle[10] = 0; strncpy(right, line + (len - 11), 10); right[10] = 0; ? or something else?
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);
}
===========================================
expected output:
(1) Wendy // assume input was
// Wendy
// John
// Jeff
expected output
(2) your jacket..cket is go...ready to a...y new one
// Assume your standard input is : your jacket is new and it was stolen so now your jacket is gone and you are ready to buy new one. Assume bold words are output FIRST TEN WORDS ...MIDDLE TEN WORDS..LAST TEN WORDS
Solution
// C code
#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)
{
done = true; // done = true; (or) exit (0) (or) something else?
}
else
{
int clen = strlen(current);
if (result == NULL)
{
result = (char*) malloc(clen*sizeof(char)); // clen?
strcpy(result, current);
}
else
{
result = (char*) realloc(current,clen*sizeof(char)); // result, strlen(result) + clen ?
strcat(result, current);
}
break;
}
}
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)+2, 10);
middle[10] = \'\\0\';
strncpy(right, line + len-11, 10);
printf(\"%s...%s...%s\ \", left, middle, right);
}
}
int 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);
break;
}
}
}
/*
output:
your jacket is new and it was stolen so now your jacket is gone and you are ready to buy new one.
your jacke...cket is go...y new one.
*/



