Question: Write a program that implements four of the string library functions using pointers: strcpy (string copy), strcat (string concatenate), strcmp (string compare), and strlen (string
Write a program 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. 1. If the strings are the same your program should print out: The two strings are the same. 2. If string 1 comes first alphabetically your program should print out: String 1 comes before string 2. 3. 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 underlined are to be entered by the user): Please enter the first string: jackhammer (underlined) Please enter the second string: Jacky(Underlined) 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 get students 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. Save your program as strings.c
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
