Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1 Your assignment is to write your own version of some of the functions in the built-in C library. As you write these functions,

Part 1

Your assignment is to write your own version of some of the functions in the built-in C library. As you write these functions, keep in mind that a string in C is represented as a char array, with the '\0' character at the end of the string. Therefore, when a string is passed as a parameter, the length of the string does not necessarily need to be passed as a separate parameter. The string itself may or may not completely fill the array.

Note that the main function that I have provided does use as it constructs test strings to pass to your functions. However, your solutions for the 5 functions below may not use any of the built-in C string functions from the library.

1. Write a function called strcmp373. This function is passed two parameters, both of which are C strings. You should use array syntax when writing this function; that is, you may use [ ], but not * or &. The function should return an int as follows:

a. A negative number if the first string is alphabetically before the second string. In C, the return value of strcmp is a number which reflects the difference in the ASCII codes for the first 2 letters which are not the same. For example, a call to strcmp("programming", "project") returns -3, since 'g' and 'j' in ASCII differ by 3.

b. Zero if the strings are the same

c. A positive number if the second string is alphabetically before the first string. Again, the number is the difference in ASCII codes between the first 2 letters which are not the same.

Here are some additional examples from the built-in C strcmp function:

strcmp("bin", "bag"); // returns 8

strcmp("computer", "game"); // returns -4

strcmp("computer", "computer"); // returns 0

strcmp("are", "area"); /* returns -97, because '\0'- a = 0-97 = -97.

Your strcmp373 function should emulate strcmp in this way; in other words, if the return value is not zero, the value of the negative or positive integer should reflect the difference in the ASCII encoding of the first differing letters in the two strings.

2.

Write a function called strcat373. The function is passed two parameters, both of which are C strings. You should use pointer syntax when writing this function (in other words, you can use * and &, but not [ ]). The function should modify the first parameter (a char array) so that it contains thee concatenation of the two strings, and return a pointer to the array. For example:

int main() {

char str1[9] = "comp";

char str2[ ] = "uter";

char str3[10] = "comp";

printf("str1 contains %s ", str1);

printf("str2 contains %s ", str2);

printf("str3 contains %s ", str3);

printf("concatenate %s and %s ", str1, str2);

strcat373(str1, str2);

printf("str1 contains %s ", str1);

printf("str2 contains %s ", str2);

printf("concetenate %s and %s ", str2, str3);

printf("str2 contains %s ", strcat373(str2, str3));

The output of this code should be

str1 contains comp

str2 contains uter

str3 contains comp

concatenate comp and uter

str1 contains computer

str2 contains uter

concetenate uter and comp

str2 contains utercomp

Note that strcat373 is guaranteed to work properly only if str1's length is big enough to contain the concatenation of the two strings. In this case, "computer" takes up 9 bytes, and since str1 is 9 bytes, the function should run properly since the array is just long enough to contain computer (plus \0). On the other hand:

char str1[] = "comp"; // only 5 bytes

char str2[] = "uter";

strcat373(str1, str2); // takes up 9 bytes and

// therefore overflows str1

Upon execution, a runtime error may occur, or (even worse) no runtime error will occur, but some other variable(s) in your program may be overwritten.

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

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students also viewed these Databases questions