Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please note that I cannot use the string library function and that it must be coded in C89, thank you! Formatting: Make sure that you

Please note that I cannot use the string library function and that it must be coded in C89, thank you!

Formatting:

Make sure that you follow the precise recommendations for the output content and

formatting: for example, do not change the text in the first problem from

Please enter a string of maximum 30 characters: to Enter string: . Your assignment will be auto-graded and any changes in formatting will result in a loss in the grade.

2.Comments:

Header comments are required on all files, for each function, and recommended throughout

the rest of the program. Points will be deducted if no header/function are included.

3.Restriction: The use of Goto statements anywhere within this program is prohibite

d. Points will be deducted if goto is used.

Part A

Write a program that asks the user to enter two strings (with a maximum length of 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 built 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.

Your program should function as follows (items underlined are to be 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

Notes:

You may not include the string library, but you may use the strlen and strcmp functions from the last project

Before terminating, your program needs to de

-allocate the space allocated for the newly created strings

Save your program as

combine_strings.c

Part 2

This program will build off the last project. You will write an interactive program for strings. The

number of strings will be provided as a command line argument. The user will enter the length of the string they are about to enter and then the string itself. After the program has read in the specified number of strings it will print the strings out and then print a menu that will allow the user to either: find the length of any string they want, compare any two strings, copy any string into another, or copy any string into another. At every iteration, your program should:

1.Print out the strings by first printing

Your strings are: and then the strings in the format

specified below

2. Print out a menu as follows:

Options:

1 Find string length

2 Compare strings

3 Copy strings

4 Concatenate strings

5 Quit

Please enter your option:

Below is a description of what the program should do for each of the above options. (items underlined are to be entered by the user):

1. Find the length of any string. You will use the strlen function. For this option, the program should ask the user to enter the number of the string they want to find the length of, as follows:

Enter a string number: 1

The length of string 1 is: 5

2. Compare any two strings.

You will use the strcmp function.

For this option, the program should ask the user to enter the numbers of two strings and then print out which string comes first, as follows:

Enter the number of the first string: 4

Enter the number of the second string: 2

String 2 comes before string 4 alphabetically.

If the strings selected by the user are the same string, your program should output: The two strings are the same.

3.Copy any two strings. You will use the strcpy function. For this option, the program should ask the user

to enter the numbers of two strings and then copy the source string into the destination string, as follows:

Enter the number of the source string: 3

Enter the number of the destination string: 6

4. Concatenate any two strings. Your function will use the strcat function. For this option, your program should ask the user to enter the numbers of two strings and then add the source string to the end of the

destination string, as follows:

Enter the number of the source string: 3

Enter the number of the destination string: 6

5.Exit the program. Your program should free any memory that was allocated before exiting.

Your program should use the four functions written in the last project but modified as necessary:

1. strlen: This function will take as a parameter a character

pointer to a string. It will return the length of

the string, not including the null terminator. This function is exactly the same as the last project.

2. strcpy: This function should take as parameters two character pointers to two strings (a source string and a destination string). First, the function must reallocate enough memory to hold the source string in the destination string. Then it will copy the source string into the destination string, including the null terminator. It will then return a pointer to the beginning of the destination string.

3. strcat: This function should take as parameters two character pointers to two strings (a source string and a destination string). First, the function should reallocate enough memory to add the source string to the end of the destination string. Then it will add the source string to the end of the destination string. It will then return a pointer to the beginning of the destination string.

4. strcmp: This function will take as parameters two character pointers to two strings (string 1 and string 2). The function then compares the two strings and if the string 1 comes first alphabetically it returns 1. If the string 2 comes first alphabetically then it

returns -1. If the strings are the same then the function returns 0. 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. This function is exactly the same as the last project.

The program should function as follows (items underlined are to be entered by the user):

> dynamic_strings 5

Enter the length of string 1: 5

Please enter string 1: first

Enter the length of string 2: 6

Please enter string 2: second

Enter the length of string 3: 5

Please enter string 3: third

Enter the length of string 4: 6

Please enter string 4: fourth

Enter the length of string 5: 5

Please enter string 5: fifth

Your strings are:

String number 1 first

String number 2 second

String number 3 third

String number 4 fourth

String number 5 fifth

Options:

1 Find string length

2 Compare strings

3 Copy strings

4 Concatenate strings

5 Quit

Please enter your option: 1

Enter a string number: 3

The length of string 3 is: 5

Your strings are:

String number 1 first

String number 2 second

String number 3 third

String number 4 fourth

String number 5 fifth

Options:

1 Find string length

2 Compare strings

3 Copy strings

4 Concatenate strings

5 - Quit

Please enter your option: 2

Enter the number of the first string: 3

Enter the number of the second string: 4

String 4 comes before string 3 alphabetically.

Your strings are:

String number 1 first

String number 2 second

String number 3 third

String number 4 -fourth

String number 5 fifth

Options:

1 Find string length

2 Compare strings

3 Copy strings

4 Concatenate strings

5 Quit

Please enter your option: 3

Enter the number of the source string: 2

Enter the number of the destination string: 5

Your strings are:

String number 1 first

String number 2 second

String number 3 third

String number 4 - fourth

String number 5 second

Options:

1 Find string length

2 Compare strings

3 Copy strings

4 Concatenate strings

5 Quit

Please enter your option: 4

Enter the number of the source string: 4

Enter the number of the destination string: 1

Your strings are:

String number 1 firstfourth

String number 2 second

String number 3 third

String number 4 fourth

String number 5 second

Options:

1 - Find string length

2 Compare strings

3 Copy strings

4 Concatenate strings

5 Quit

Please enter your option: 5

Notes:

If the user enters an invalid menu option, the program should print Invalid Option. then it should print the menu again and ask for a valid option, as follows:

Invalid Option.

Your strings are:

String number 1 first

String number 2 second

String number 3 third

String number 4 fourth

String number 5 fifth

Options:

1 Find string length

2 Compare strings

3 Copy strings

4 - Concatenate strings

5 Quit

Please enter your option:

You may NOT include the string library. Inclusion of the string library will result in a

grade of zero for this project. Use the functions from project 7 and make any necessary changes.

In the last project you used static memory allocation and assumed a max string size of 50 characters. This is no longer the case. You must dynamically allocate memory for all your strings and it is your responsibility to free the memory before the program ends.

For this program, you must take the number of strings in as a command line argument. This number will be

stored as a string not as an integer. To get the integer value of a string use the atoi(argv[1])function from stdlib. This function takes as a parameter a string and gives the

equivalent integer value.

Save your program as

dynamic_strings.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

Students also viewed these Databases questions

Question

explain testing of real - time systems

Answered: 1 week ago