Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program in C that implements four of the string library functions using pointers: strcpy (string copy), strcat (string concatenate), strcmp (string compare), and

Write a program in C that implements four of the string library functions using pointers: strcpy (string copy), strcat (string concatenate), strcmp (string compare), and strlen (string length). First, the program will ask the user to enter two strings with a maximum of 50 characters each. The program will then output the length of each string. Then, it will output which string comes first alphabetically. The program will then add string 1 to string 2 and output the two strings. Finally, the program will copy string 1 into string 2 and print the two strings again. Your program should: 1. Using the strlen function, calculate and print out the length of each string. 2. Using the strcmp function, print out which string comes first alphabetically. a. If the strings are the same your program should print out: The two strings are the same. b. If string 1 comes first alphabetically your program should print out: String 1 comes before string 2. c. If string 2 comes first alphabetically your program should print out: String 2 comes before string 1. 3. Using the strcat function, add string 1 to the end of string 2 and print out string 1 and 2 4. Using the strcpy function, copy string 1 into string 2 and print out string 1 and 2

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

Please enter the first string: jackhammer Please enter the second string: Jacky The length of string 1 is: 10 The length of string 2 is: 5 String 1 comes before string 2 alphabetically. String 1 after concatenation: jackhammer String 2 after concatenation: Jackyjackhammer String 1 after copying: jackhammer String 2 after copying: jackhammer

Your program should implement and use the following functions:

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.

strcpy: This function should take as parameters two character pointers to two strings (a source string and a 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.

strcat: This function should take as parameters two character pointers to two strings (a source string and a 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.

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

Note: You may NOT include the string library. The point of this project is to work with pointers and their use in strings. Inclusion of the string library will result in a grade of zero for this project. (You must write all the functions yourself) Your functions must use pointers and pointer arithmetic. You are not allowed to use square brackets to move along the strings. You can assume that all strings will be just alphabetic characters. The strings will not contain any spaces.

Note:

1. Formatting: Make sure that you follow the precise recommendations for the output content and formatting: for example, do not change the text of the problem from Enter the first string: to Enter string: . Your assignment will be auto-graded and any change 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 comments are included.

3. Restriction: The use of goto statements anywhere within this program is prohibited. Points will be deducted if goto is used.

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

Postgresql 16 Administration Cookbook Solve Real World Database Administration Challenges With 180+ Practical Recipes And Best Practices

Authors: Gianni Ciolli ,Boriss Mejias ,Jimmy Angelakos ,Vibhor Kumar ,Simon Riggs

1st Edition

1835460585, 978-1835460580

More Books

Students also viewed these Databases questions