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

Write a program that sorts a series of words entered by the user: Each word is n

ID: 664645 • Letter: W

Question

Write a program that sorts a series of words entered by the user:

Each word is no more than 20 characters long and the program stops reading when the user enters an empty word (i.e., presses enter without entering a word). Store each word in a dynamically allocated string using an array of pointers to keep track of the string, as in the remind2.c program in section 17.2 of C Progrmamming a Modern Approach. After all words have been read, sort the array (using any sorting technique) and then use a loop to print the words in sorted order. Use the read_line function below to read each word as in remind2.c

Here's the read_line function:

Here is my code:

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TOTALWORDS 20
#define LENGTH 20


int read_line(char str[], int n);

int main(){

   int i;
   int length;
   int x = 0;
   char word[LENGTH + 1];
   char *words[TOTALWORDS];

   for(;;){
       if(x == TOTALWORDS){
           printf("No space ");
           break;
       }
  
       printf("Enter word: ");
       length = read_line(word, TOTALWORDS);

       if(length == 0){
           break;
       }

       *(words + x) = malloc(length);
      
       if(*(words + x) == NULL){
           printf("Failure... Exiting ");
           exit(0);
       }

       strcpy(*(*length + x), word);
       ++x;
   }

   printf("In sorted order:");
   for(i = 0; i < x; ++i){
       printf("%s ", (*(*length + i)));
       free(*(*length + i));
   }

   return 0;

}


int read_line(char str[], int n){
   int ch, i = 0;
  
   while ((ch = getchar()) != ' ') {
       if (i < n){
           str[i++] = ch;
       }
   }
  
   str[i] = '';

   return i;
}

Please help.

#include <stdio.h>

#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define TOTALWORDS 20
#define LENGTH 20


int read_line(char str[], int n);

int main(){

   int i;
   int length;
   int x = 0;
   char word[LENGTH + 1];
   char *words[TOTALWORDS];

   for(;;){
       if(x == TOTALWORDS){
           printf("No space ");
           break;
       }
  
       printf("Enter word: ");
       length = read_line(word, TOTALWORDS);

       if(length == 0){
           break;
       }

       *(words + x) = malloc(length);
      
       if(*(words + x) == NULL){
           printf("Failure... Exiting ");
           exit(0);
       }

       strcpy(*(*length + x), word);
       ++x;
   }

   printf("In sorted order:");
   for(i = 0; i < x; ++i){
       printf("%s ", (*(*length + i)));
       free(*(*length + i));
   }

   return 0;

}


int read_line(char str[], int n){
   int ch, i = 0;
  
   while ((ch = getchar()) != ' ') {
       if (i < n){
           str[i++] = ch;
       }
   }
  
   str[i] = '';

   return i;
}

Explanation / Answer

C program code:


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

#define TOTALWORDS 20
#define LENGTH 20

int read_line(char *, int);

int main(void)
{ /*variable declaration*/
   char *words[LENGTH];
   char word[ TOTALWORDS + 1];
   int i, length, index = 0;
   for(;;)
   {
       if(index == LENGTH)
       {
           printf(" ..vector is full.. ");
           break;
       }
       /*Getting input from user*/
       printf("Enter word: ");
       length = read_line(word, TOTALWORDS);
       if(length == 0)
           break;
       *(words + index) =malloc(length);
       if(*(words + index) == NULL)
       {
           printf(" malloc failed %d of words! ", index);
           exit(EXIT_FAILURE);
       }
       strcpy(*(words + index), word);
       ++index;
   }
   /*display output*/
   printf(" In sorted order:");
   for(i = 0; i < index; ++i)
{
       printf(" %s", *(words + i));
       free(*(words + i));
       printf(" ");
   }
   return 0;
}

int read_line(char *str, int n)
{
   int ch, i = 0;
  
   while(!isspace(ch = getchar()))
       if(i < n)
           *(str + i++) = ch;
  
   while(ch != ' ')
       ch = getchar();
      
   *(str + i) = '';
   return i;
}

Program output:

Enter word: foo

Enter word: bar

Enter word: baz

Enter word: quux

Enter word:

In sorted order: foo bar baz quux

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