Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

POINTER SORTER Need a program in a C language. 0. Introduction In this assignment, you will practice programming with C pointers and using dynamic memory.

POINTER SORTER Need a program in a C language.

0. Introduction In this assignment, you will practice programming with C pointers and using dynamic memory. Manipulation of dynamic memory and the pointer construct are two defining characteristics of the C language. They are powerful tools but can be fiddly to work with. For your first assignment, you will get a little practice working with both. You will write a rather simple program to sort words given in a single input string. Your code will take a single, long input string that contains all words to be sorted, break them out into individual strings, sort them, and output them one per line in descending alphabetical order. You will not know the length of the input string or its constituent strings beforehand, or how many there are, so you will not know how much memory to allocate or how many pointers you'll need to handle the input string. These are two fundamental issues you must consider.

1. Implementation The input string will contain a series of component strings, e.g.: thing stuff otherstuff blarp. You must extract and sort these strings. The component strings will be separated by non-alphabetic characters. You can consider any non-alphabetic character a separator, e.g.: thing1stuff3otherstuff,blarp is equivalent to the input string above. Be careful not to run off of the end of the input string as you are scanning it. Your first task should be to determine its length. You have three major segments to think about: 0. How to deal with component strings of unknown lengths 1. How to deal with an unknown number of component strings 2. How to sort an unknown number strings of an unknown length

1.0 Unknown Lengths You can only ask for a given amount of memory to store some data in C. Since you do not know the lengths of the component strings when you start, you'll need to discover them so that you can request memory to store them in with malloc(). Any time you do not know exactly how long a process will take, but know when are you done, is often an excellent time for a loop. You should use pseudocode and rough things out first. while( haven't fallen off the input string ) { .. read from input string while( haven't found a separator and haven't fallen off the input string ) { .. read from input string .. advance to next character } // either fell off of input string, or found a separator if( found a separator ) { .. copy the component string out of the input string } You will need to move along the input string testing characters until you find a separator. Remember, any non-alphabetic character is a separator (can't just split()). Once you find a separator, you can determine the length of the string it marked the end of. You can then request some memory to store that string with malloc() and copy the string over with memcpy(). You now need to hold the pointers to your component string copies in a data structure.

1.1 Unknown Number Since you do not know how many total component strings will be in a single input string, you need to be ready with an extensible data structure. There are several ways to approach this problem. You could build a link list, or manage an array to increase its size as needed, or implement a red/black tree. However you solve it, the data structure you use to hold your component string pointers must stretch to hold any number of them.

1.2 Sorting Since you must discover the end of a component string before you copy it out of the input string and into your list of strings, you must insert them into your list one by one. You might find it easier to sort them and insert them into the list in sorted order. If not, you can easily build the list, but will need to sort all of it at once. When writing your sorting function, remember that it will not know the lengths of the strings it is comparing. Be careful not to fall off the ends of your component strings when comparing them.

Examples ./pointersorter thing stuff otherstuff blarp

blarp

otherstuff

stuff

thing

./pointersorter thing1stuff3otherstuff,blarp

blarp

otherstuff

stuff

thing

4. Additional Be careful to output your strings in descending alphabetical order Treat capital letters as greater than lower case they should come before lowercase: for words: aand, aAnd, Aand, Aand output as:

AAnd

Aand

aAnd

aand

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_2

Step: 3

blur-text-image_3

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 And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions

Question

Evaluate three pros and three cons of e-prescribing

Answered: 1 week ago

Question

3. How would you address the problems that make up the situation?

Answered: 1 week ago