Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

  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:

    1. 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' - 'j' in ASCII is -3.

    2. Zero if the strings are the same

    3. 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 examples of how your code should work:

    strcmp373("bin", "bag"); // returns 8 strcmp373("computer", "game"); // returns -4strcmp373("computer", "computer"); // returns 0strcmp373("are", "area"); // returns -97, because '\0'- a = 0-97 = - 97.

  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, often called the destinationarray) so that it contains the concatenation of the two strings, and returns a pointer to the destination array. For example:

    int main() { char str1[9] = "comp"; char str2[ ] = "uter"; char *str3; strcat373(str1, str2); // prints computer twice printf("str1 contains %s str3 contains %s ,

    str1, str3);

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 containcomputer (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 achar 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')); printf("%s", strchr373(s, 'c'); printf("%d", strchr373(s, 'd'); 
// prints bcbc // prints cbc // prints 0 
  1. 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 ", s1); 

    The output of the above code should be syst.

  2. Write a function called strncat373. It performs string concatenation (just like strcat373), except that it limits the number of characters that may be copied from src onto the end of dest.

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

Fundamentals Of Database Management Systems

Authors: Mark L. Gillenson

3rd Edition

978-1119907466

More Books

Students also viewed these Databases questions