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 by rewriting the line.c file so that

ID: 3774654 • Letter: M

Question

Modify the justify program of Section 15.3 by rewriting the line.c file 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 first word. This variable will store a null pointer whenever the line is empty.

/* line.c (Chapter 15, page 364) */

#include <stdio.h>

#include <string.h>

#include "line.h"

#define MAX_LINE_LEN 60

char line[MAX_LINE_LEN+1];

int line_len = 0;

int num_words = 0;

void clear_line(void)

{

line[0] = '';

line_len = 0;

num_words = 0;

}

void add_word(const char *word)

{

if (num_words > 0) {

line[line_len] = ' ';

line[line_len+1] = '';

line_len++;

}

strcat(line, word);

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;

for (i = 0; i < line_len; i++) {

if (line[i] != ' ')

putchar(line[i]);

else {

spaces_to_insert = extra_spaces / (num_words - 1);

for (j = 1; j <= spaces_to_insert + 1; j++)

putchar(' ');

extra_spaces -= spaces_to_insert;

num_words--;

}

}

putchar(' ');

}

void flush_line(void)

{

if (line_len > 0)

puts(line);

}

Explanation / Answer

/ * Copy this Program as Name it as line.c , Keep line.h also in the same location as line.c. Compile and Run ..It will ask you // to add words which is stored in Line as well as Linked List
// Its a complete working Code with free of errors, Let me know if you find any issues. */

#include<stdio.h>
#include <stdlib.h>

#include <string.h>

#include <stdio.h>

#include <string.h>

#include "line.h"

struct ListNode

{

char word[MAX_LINE_LEN];

struct ListNode* next;

};

void printList(struct ListNode *head)

{

while(head != NULL)

{

printf("%s ",head->word);

head = head->next;

}

}

void deleteList(struct ListNode *head)

{

struct ListNode* temp = head;

while(temp != NULL)

{

head = head->next;

free(temp);

temp = head;

}

}

void insertNode(struct ListNode** head, char word[])

{

struct ListNode* node =(struct ListNode*) malloc(sizeof(struct ListNode));

struct ListNode* temp;

strcpy(node->word , word);

node->next = NULL;

if (*head == NULL)

{

node->next = *head;

*head = node;

}

else

{

temp = *head;

while(temp->next!=NULL)

temp = temp->next;

temp->next = node;

}

}

int main(int argc, char *argv[]) {

struct ListNode *head = NULL;

//As of Now I am adding only Three words , Increase it to add more than 3 words

printf("Enter Three words , As of now we are only adding only three words , Increase it to add more than 3 words ");

   for(int i=0;i<3;++i) //Increase the word Limit to more than 3

{

char str[MAX_LINE_LEN];

printf("Enter a Word ");

scanf("%s", str);

printf("You entered: %s ", str);

add_word(str);

insertNode(&head,str);

   }

   printf(" ");

printf("Printing The line stored in line array ");

write_line();

printf("Printing The line stored in the Linked List ");

printList(head);

deleteList(head);

return 0;

}

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