Question: Your assignment is to write your own version of some of the functions in the built-in C library. In writing your code you may not

Your assignment is to write your own version of some of the functions in the built-in C library.

In writing your code you may not use the built-in C string functions from the library.

1. Write a function called strcmp406. 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:

a. A negative number if the first string is alphabetically before the second string. In C, the return value of strcmp returns 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: strcmp406("bin", "bag") should return 8 strcmp406("computer", "game"); should return -4 strcmp406("computer", "computer"); should return 0 strcmp406("are", "area"); should return -97, because the '\0' character (at the end of "are", and represented by the number 0) is compared with a (ASCII a is 97).

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

#include #include // you may NOT include this in hw2.h or hw2.c #include "hw2.h"

int main() { char str1[100]; char str2[100]; char small[4]; strcpy(str1, "programming"); strcpy(str2, "project"); strcpy(str1, "bin"); strcpy(str2, "bag");

printf("Problem 1 ");

printf("strcmp406(\"%s, %s\") = %d ", str1, str2, strcmp406(str1, str2)); strcpy(str1, "computer"); strcpy(str2, "game"); printf("strcmp406(\"%s\", \"%s\") = %d ", str1, str2, strcmp406(str1, str2)); strcpy(str1, "computer"); strcpy(str2, "computer"); printf("strcmp406(\"%s\", \"%s\") = %d ", str1, str2, strcmp406(str1, str2)); strcpy(str1, "are"); strcpy(str2, "area"); printf("strcmp406(\"%s\", \"%s\") = %d ", str1, str2, strcmp406(str1, str2));

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!