Write a program that sorts a series of words entered by the user: Each word is n
ID: 664644 • 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
#include <stdio.h>
#include <stdlib.h>
int int_compare(const void* p, const void* q);
int main()
{
int n, i;
int *a;
printf("Enter the length of the array: ");
scanf("%d", &n);
a = malloc(n*sizeof(int));
for(i = 0; i < n; i++)
{
printf("Enter a number: ");
scanf("%d", &a[i]);
}
qsort(a, n, sizeof(int), int_compare);
printf("In sorted order: ");
for(i = 0; i < n; i++)
printf("%d ", a[i]);
printf(" ");
return 0;
}
int int_compare(const void* p, const void* q){
int n1 = *(int *) p;
n1 = *(int *) p;
int n2 = *(int *) q;
if (n1 < n2)
return -1;
if (n1 == n2)
return 0;
return 1;
}
int read_line(char str[], int n)
{
int ch, i = 0;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.