Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1 (Combining strings): Write a program that asks the user to enter two strings (of length at most 30) and then performs one of

Problem 1 (Combining strings):

Write a program that asks the user to enter two strings (of length at most 30) and then performs one of two possible operations provided as command line arguments to the program. The possible command line options and the associated operations are as follows:

Option: -i Operation: if given this option, the program should create a new string that is built by interspersing the two strings, by alternatively placing one character at a time from each string. When one of the strings exhausted, the program should copy the remaining of the characters from the second string. This functionality should be implemented in a function called intersperse, which takes as parameters the two strings and returns a pointer to the newly created string. Your function should use dynamic memory allocation to generate the new string.

For example, if string 1 is abcde and string 2 is 1234567, the resulting string should be a1b2c3d4e567.

Option: -w Operation: if given this option, the program should create a new string that is build by concatenating the two given strings, in which there has been a * character inserted in between every character in those strings. There should be a * character between the two strings, but no such character at the end of string 2. This functionality should be implemented in a function called widen_stars, which takes as parameters the two strings and returns a pointer to the newly created string. Your function should use dynamic memory allocation to generate the new string.

For example, if string 1 is abcde and string 2 is 1234567, the resulting string should be a*b*c*d*e*1*2*3*4*5*6*7.

Here is an example of exactly how the program should behave (items underlined are entered by the user):

> combine_strings -i

> Please enter a string of maximum 30 characters: abcde

> Please enter a string of maximum 30 characters: 1234567

> The combined string is: a1b2c3d4e567

or

> combine_strings -w

> Please enter a string of maximum 30 characters: abcde

> Please enter a string of maximum 30 characters: 1234567

> The combined string is: a*b*c*d*e*1*2*3*4*5*6*7

Additional constraints:

Your program should use the exact form and spacing of the user prompts and output as indicated above

Before terminating, your program should de-allocate the space allocated for the newly created strings.

Code for this program should be in a file named combine_strings.c

Problem 2

You will write a program that uses a pre-defined array of strings (the planets array discussed in class) and sorts the names of the planets in either alphabetical or reverse alphabetical order. The program will use command line arguments to read in the order in which the strings should be printed: -o a for alphabetical and -o r for reverse alphabetical order. Your program should follow the constraints below:

- Implement a function string_compare, which takes as parameters two strings (str1 and str2) and returns 0 if str1 is prior to str2 alphabetically, 1 if str2 is prior to str1 alphabetically and -1 if str1 is identical to str2. The function should compare the two strings one character at a time and return the appropriate value as soon as a difference is noticed between the strings.

- Use the above function to sort the names of the planets by using the following strategy:

for i = 0 to size of planets array - 1

for j = size of planets array - 1 down to i + 1

if planets[j] is prior to planets[j -1] alphabetically

then exchange planets[j] with planets[j-1]

The exchange in the last line above will exchange the pointers stored in planets[j] and planets[j-1]. For strings that are identical (assuming that the names of the planets would be changed), there should be no exchange needed.

Note: the algorithm above sorts in alphabetical order. For reverse alphabetical order, the if test should be change accordingly.

Your program should function as follows:

./planets o a

The planets in alphabetical order are: Earth, Jupiter, Mars, Mercury, Neptune, Pluto, Saturn, Uranus, Venus

Alternatively, for reverse alphabetical order:

./planets o r

The planets in reverse alphabetical order are: Venus, Uranus, Saturn, Pluto, Neptune, Mercury, Mars, Jupiter, Earth

Your program should be saved in a file called planets_sort.c.

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

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

More Books

Students also viewed these Databases questions

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago