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

C89 Programming Help Write a program that sorts a series of words entered by the

ID: 664264 • Letter: C

Question

C89 Programming Help

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

Assume that each word is no more than 20 characters long. Stop 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 your book. 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. HINT: Use the read_line function to read each word as in remind2.c

Explanation / Answer

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

int main()
{
char *storenames;
char c[20];
char temp[20];
int i,j,count=0;;
storenames = calloc( 20, sizeof(char) );//dynamic memory allocation
if( storenames== NULL )
{
printf("memory not allocated ");
}
else
{
while(true){//reading words until empty string entered
printf(" Enter word:");
scanf("%s",c);
if(storenames[count] == ''){
break;
}
strcpy( storenames,c);
count++;
}
}
for (i = 0; i < count - 1 ; i++)//sorting names using bubble sort
{
for (j = i + 1; j < count; j++)
{
if (strcmp(storenames[i], storenames[j]) > 0)
{
strcpy(temp, storenames[i]);
strcpy(storenames[i], storenames[j]);
strcpy(storenames[j], temp);
}
}
}
  
printf(" Printing sorting storenamess :");//finally printing names
for (i = 0; i < count; i++)
{
printf("%s ",storenames[i]);
}
free(storenames);
}

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