Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your assignment is to write your own version of some of the functions in the bui

ID: 3819224 • Letter: Y

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 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 '' 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 <stdio.h>
#include <string.h> // 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));

Explanation / Answer

//hw1.h

int strlen(char str1[]);
int strcmp406(char str1[], char str2[]);

-------------------------------

//hw2.cpp

int strlen(char str1[])
{
   int i = 0;
   while (str1[i++] != '');
   return i;
}

int strcmp406(char str1[], char str2[])
{
   int i;
   int first = 1;
   int ret = 0;
   if (strlen(str1) > strlen(str2))
   {
       for (i = 0; i < strlen(str2); i++)
       {
           if (str1[i] != str2[i])
           {
               if (first == 1)
                   ret = str1[i] - str2[i];
               first++;
           }
       }
   }
   else
   {
       first = 1;
       for (i = 0; i < strlen(str1); i++)
       {
           if (str1[i] != str2[i])
           {
               if (first == 1)
                   ret = str1[i] - str2[i];
               first++;
           }
       }

   }

   return ret;

}

-----------------------------------------

//main program

#include <stdio.h>
#include<string.h>
#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));
}

---------------------------------------------------------------

//output

Problem 1

strcmp406("bin, bag") = 8
strcmp406("computer", "game") = -4
strcmp406("computer", "computer") = 0
strcmp406("are", "area") = -97

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote