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

Modify the justify program of Section 15.3 from the textbook by rewriting the li

ID: 3646768 • Letter: M

Question

Modify the justify program of Section 15.3 from the textbook by rewriting the line.c le so that it stores the current line in a linked list. Each node in the list will store a single word. The line array will be replaced by a variable that points to the node containing the rst word. This variable will store a null pointer whenever the line is empty.. line.c : #include #include #include #include "line.h" #define MAX_LINE_LEN 60 typedef struct node { char *word; struct node *next; } Node; Node *first = NULL; Node *last = NULL; char line[MAX_LINE_LEN +1]; int line_len = 0; int num_words = 0; void clear_line(void) { Node *temp; while (first != NULL) { temp = first; first = first->next; free(temp); } line_len = 0; num_words = 0; } void add_word (const char *word) { if (num_words > 0) { first = malloc(sizeof (Node)); first->word = malloc(strlen(word) + 1 ); strcpy(first->word, word); first->next = NULL; last = first; } else { last->next = malloc( sizeof (Node)); last->next->word = malloc(strlen(word) + 1 ); strcpy(last->word, word); last = last->next; } line_len += strlen(word); num_words++; } int space_remaining(void) { return MAX_LINE_LEN - line_len; } void write_line(void) { int extra_spaces, spaces_to_insert, i, j; extra_spaces = MAX_LINE_LEN - line_len ; last = malloc(sizeof (Node)); for ( i = 0; i < line_len; i++) { spaces_to_insert = extra_spaces / ( num_words - 1) ; for ( j = 1; j <= spaces_to_insert + 1; j++) putchar(' '); while (last->word == ' '){ extra_spaces -= spaces_to_insert ; num_words --; last = last->next; } puts(last->word); } putchar( ' ' ); } void flush_line (void) { while( first != NULL ){ puts(first->word); first = first->next; } } line.h : #ifndef LINE_H #define LINE_H /************************************************************************ clear_line: Clears the current line. ************************************************************************/ void clear_line (void); /************************************************************************ add_word: Adds word to the end of the current line. If this is not the first word on the line, Puts one space before word. *************************************************************************/ void add_word (const char *word); /************************************************************************* space_remaining: returns the number of characters left in the current line. *************************************************************************/ int space_remaining (void); /************************************************************************* write_line: writes the current line with justification. *************************************************************************/ void write_line(void); /************************************************************************* flush_line: writes the current line without justification. If the line is empty, does nothing. *************************************************************************/ void flush_line (void); #endif word.c: #include #include "word.h" int read_char(void) { int ch = getchar(); if (ch == ' '|| ch == ' ' ) return ' '; return ch; } void read_word(char *word, int len) { int ch, pos = 0 ; while ((ch = read_char()) == ' ') ; while (ch != ' ' && ch != EOF) { if ( pos < len ) word[pos++] = ch; ch = read_char(); } word[pos] = ''; } justify.c : /* formats a file of text */ #include #include "line.h" #include "word.h" #define MAX_WORD_LEN 20 int main(void) { char word[MAX_WORD_LEN+2]; int word_len; clear_line(); for (;;) { read_word(word, MAX_WORD_LEN+1); word_len = strlen(word); if (word_len == 0) { flush_line(); return 0; } if (word_len > MAX_WORD_LEN) word[MAX_WORD_LEN] = '*'; if (word_len + 1 > space_remaining()) { write_line(); clear_line(); } add_word(word); } }

Explanation / Answer

Your program has written here like an essay. So, it is impossible for me to paste it in the C-command windows. Please copy paste is from the C-window step by step. Otherwise it will take days to understand above essay.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote