Question: 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

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.

3. Write a function called strchr373. It is passed 2 parameters: a string and a char Here is the prototype for the function: char *strchr373(char str[], char ch); The function should return a pointer to the first instance of ch in str. For example: int main { char s[ ] = "abcbc"; printf("%s", strchr373(s, 'b')); // prints bcbc printf("%s", strchr373(s, 'c'); // prints cbc printf("%d", strchr373(s, 'd'); // prints 0 }

4. Write a function called strncpy373. It is passed 3 parameters: 2 strings and the length of the destination array (the first parameter). The intent of including a third parameter is to prevent overflow of the destination array in case the source array (the second parameter) contains a string that is too long to be stored in the destination. strncpy373 returns a pointer to the destination array. For example: char s1[] = "comp"; char s2[] = "systems"; strncpy373(s1, s2, 4); printf("%s "); The output of the above code should be syst.

5. Write a function called strncat373. Similarly to the previous functions, if properly used strncat373 prevent overflow of the destination array in case the source array (the second parameter) contains a string that is too long to be stored in the destination. As the functions name indicates, this time concatenation is performed on the source string rather than copying (and overwriting) that string.

int strcmp373(char str1[], char str2[]) { return 0; // replace this }

char *strcat373(char dest[], char src[]) { // add code here

return dest; }

char *strchr373(char str[], char c) { return str; // replace this }

char *strncpy373(char dest[], char src[], int n) { // add code here

return dest; }

char *strncat373(char dest[], char src[], int n) { return dest; // replace this }

(below is the main function)

#include #include #include "hw3.c" /* #include "strcmp373.c" #include "strcat373.c" #include "strchr373.c" #include "strncpy373.c" #include "strncat373.c" */

int main() {

// 1. strcmp373 printf("Problem 1. "); int c1, c2, c3, c4; c1 = strcmp373("bin", "bag"); // returns 8 c2 = strcmp373("computer", "game"); // returns -4 c3 = strcmp373("computer", "computer"); // returns 0 // returns -97 the difference between '\0' and 'a c4 = strcmp373("are", "area"); printf("%d %d %d %d ", c1, c2, c3, c4);

// 2. strcat373 printf("Problem 2. "); char stra[10] = "comp"; char strb[ ] = "uter"; char strc[10] = "comp"; printf("stra contains %s ", stra); printf("strb contains %s ", strb); printf("strc contains %s ", strc); printf("concatenate %s and %s ", stra, strb); strcat373(stra, strb); printf("stra contains %s ", stra); printf("strb contains %s ", strb); printf("concetenate %s and %s ", strb, strc); printf("str2 contains %s ", strcat373(strb, strc));

// 3. strchr373 printf("Problem 3. "); char words[] = "off-campus and on-campus housing"; char *c= strchr373(words, '-'); printf("Looking for '-' in %s Found at location %s ", words, c); char *d; d = strchr373(&words[5], '-'); printf("Looking for '-' in %s Found at location %s ", c+1, d);

// 4. strncpy373 printf("Problem 4 "); char s1[] = "comp"; char s2[] = "systems"; printf("Using strncpy into %s from %s, ", s1, s2); printf("but only allowing 4 characters "); strncpy373(s1, s2, 4); printf("Result is s1 %s and s2 %s ", s1, s2);

// 5. strncat373 printf("Problem 5 "); char strn1[100] = "comp"; // this is where all the concatenation is char strn2[] = "uter "; // going on so I made it bigger char strn3[] = "science"; strncat(strn1, strn2, 5); strncat(strn1, strn3, 7); char strn4[15] = "comp"; // a smaller array. The entire concatenation strncat(strn4, strn2, 5); // can't fit strncat(strn4, strn3, 5); printf("strn4 is %s ", strn4); }

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!