QuestionQuestion

Your assignment is to write your own version of some of the functions in the built-in <string.h> C library.
As you write these functions, keep in mind that a string in C is represented as a char array, with the '' 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 <string.h> 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 <string.h> 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' - 'j' in ASCII is -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 examples of how your code should work:

strcmp373("bin", "bag"); // returns 8
strcmp373("computer", "game"); // returns -4
strcmp373("computer", "computer"); // returns 0
strcmp373("are", "area"); // returns -97, because ''- ‘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 destination array) 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 %snstr3 contains %sn, 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 contain “computer” (plus ‘’).
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("%sn", s1);
The output of the above code should be syst.

5. Write a function called strncat373. Similarly to the previous functions, if properly used strncat373 prevents 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 function’s name indicates, this time concatenation is performed on the source string rather than copying (and overwriting) that string.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

*/
#include <stdio.h>

// returning 0 means that str1 and str2 match character-by-character
// returning a neg int means that str1 is lexicographically before str2
// returning a pos int means that str1 is lexicographically after str2
int strcmp373(char str1[], char str2[]) {
    int i = 0;
    int j = 0;
    while (str1[i] == str2[j]) {
       if (i < sizeof(str1)-1)
            i++;
       if (j < sizeof(str2)-1)
            j++;
       if (i == sizeof(str1) - 1 && j == sizeof(str2) - 1)
            break;
    }
    return str1[i] - str2[j]; // replace this
}

// This is the string concatenation function. It returns a C...

By purchasing this solution you'll be able to access the following files:
hw2.c, hw2main.c and OUTPUT.png.

$60.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats