Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Bubble Sort For this task, you will implement the primary data structure as a 2-dimensional array Strings[NUM][LEN], where NUM is the number of strings, and

Bubble Sort

For this task, you will implement the primary data structure as a 2-dimensional array Strings[NUM][LEN], where NUM is the number of strings, and LEN is the maximum length allowed for the string: the first index i gives you the i-th string, and the second index j gives you the j-th character within that string.

Using the giving code, implement bubble sort of strings. Your code must strictly follow these specifications:

  • Use fgets() to read each string from the input, one string per line. Use LEN as an argument to fgets() to ensure that an input line that is too long does not exceed the bounds imposed by the string's length. Note that the newline and NULL characters will be included in LEN, so the maximum number of printable characters in the string will actually be LEN-2.
  • The comparison of two strings must be done by checking them one character at a time, without using any C string library functions. That is, write your own loop to do this.
  • The swap of two strings must be done by copying them (using a temp variable of your choice) one character at a time, without using any C string library functions. That is, write your own loop to do this.
  • You are allowed to use C library functions for printing strings, e.g., fputs(), puts(), printf(), etc.
  • You are allowed to use the C library function strlen() to calculate the length of a string.

Compile and run your program on inputs of your choice, and use < and > to redirect the input of your choice and output.

/* Example: bubble sort strings in array */

#include /* Need for standard I/O functions */ #include /* Need for strlen() */

#define NUM 30 /* number of strings */ #define LEN 1200 /* max length of each string */

int main() { char Strings[NUM][LEN];

printf("Please enter %d strings, one per line: ", NUM);

/* Write a for loop here to read NUM strings.

Use fgets(), with LEN as an argument to ensure that an input line that is too long does not exceed the bounds imposed by the string's length. Note that the newline and NULL characters will be included in LEN. */

puts(" Here are the strings in the order you entered:");

/* Write a for loop here to print all the strings. */

/* Bubble sort */ /* Write code here to bubble sort the strings in ascending alphabetical order

Your code must meet the following requirements: (i) The comparison of two strings must be done by checking them one character at a time, without using any C string library functions. That is, write your own while/for loop to do this. (ii) The swap of two strings must be done by copying them (using a temp variable of your choice) one character at a time, without using any C string library functions. That is, write your own while/for loop to do this. (iii) You are allowed to use strlen() to calculate string lengths. */

/* Output sorted list */ puts(" In alphabetical order, the strings are:"); /* Write a for loop here to print all the strings. Feel free to use puts/printf etc. for printing each string. */

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions