Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this task, you will implement the primary data structure as a 1-dimensional string array char * Strings[NUM], where NUM is the number of strings,

For this task, you will implement the primary data structure as a 1-dimensional string array char * Strings[NUM], where NUM is the number of strings, and the length of strings will be determined dynamically.

Using the starter file bubble.c, implement bubble sort of strings in dictionary order. 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 LEN is the size of buffer fgets() uses. Each string in the array Strings must not waste space, that is, if a line is only abc, then the storage for this string in the array Strings should be 4 bytes instead of LEN.

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 or implement your own mystrcmp() to do this.

The swap of two strings must be done by swapping two pointers, no copying of chars are allowed. Write your own swap function to do this (How would the prototype of swap-two-pointers look like?).

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.

No other string libraries/functions are allowed. [In response to students question: you can use malloc/free from stdlib.h for sure]

Compile and run your program on inputs of your choice, and you can use to redirect the input of your choice and output if you would like your code to read inputs from a file instead of from the keyboard.

image text in transcribed

bubble.c x /* Example: bubble sort strings in array */ 1 2 3 #include /* Need for standard I/O functions */ #include /* Need for an */ #define NUM 30 #define LEN 1200 /* number of strings */ /* max length of each string */ int main() 4 5 6 7 8 9 10 ll 12 13 14 15 16 17 18 19 char * Strings [NUM]; printf("Please enter $d strings, one per line: ", NUM); /* Write a for loop here to read NUM strings. Use faet, 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. However, each string stored in array Strings must not waste space. That is, only the buffer used by fast to temporary store the string read from input stream needs to be LEN bytes long. Note that the newline and NULL characters will be included in LEN. 20 21 22 23 24 25 26 27 28 29 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 31 32 33 34 35 36 37 38 39 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) Implement a swap function to swap two strings by swapping pointers without copying any chars. You are not allowed to use any C library functions in swap. (iii) You are allowed to use them to calculate string lengths. 40 41 42 43 /* Output sorted list */ puts (" In alphabetical order, the strings are:"); /* Write a for loop here to print all the strings. Feel free to use putsainte etc. for printing each string. 45 46 47 48 49 50 51 bubble.c x /* Example: bubble sort strings in array */ 1 2 3 #include /* Need for standard I/O functions */ #include /* Need for an */ #define NUM 30 #define LEN 1200 /* number of strings */ /* max length of each string */ int main() 4 5 6 7 8 9 10 ll 12 13 14 15 16 17 18 19 char * Strings [NUM]; printf("Please enter $d strings, one per line: ", NUM); /* Write a for loop here to read NUM strings. Use faet, 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. However, each string stored in array Strings must not waste space. That is, only the buffer used by fast to temporary store the string read from input stream needs to be LEN bytes long. Note that the newline and NULL characters will be included in LEN. 20 21 22 23 24 25 26 27 28 29 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 31 32 33 34 35 36 37 38 39 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) Implement a swap function to swap two strings by swapping pointers without copying any chars. You are not allowed to use any C library functions in swap. (iii) You are allowed to use them to calculate string lengths. 40 41 42 43 /* Output sorted list */ puts (" In alphabetical order, the strings are:"); /* Write a for loop here to print all the strings. Feel free to use putsainte etc. for printing each string. 45 46 47 48 49 50 51

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

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions