Question: Your task in this assignment is to complete your own versions of these five commonly used standard string functions. Your version: int mystrlen( const char

Your task in this assignment is to complete your own versions of these five commonly used standard string functions.

Your version: int mystrlen( const char *s) int mystrcmp( const char *s1, const char *s2) char *mystrcpy( char *s1, const char *s2) char *mystrcat( char *s1, const char *s2) C standard string library functions: int strlen( const char *s) int strcmp( const char *s1, const char *s2) char *strcpy( char *s1, const char *s2) char *strcat( char *s1, const char *s2)
char *mystrrchr(const char *pc, int iChar);

char *strrchr(const char *pc, int iChar);

Each of your functions should have the same behavior as the corresponding above C standard string function. For example, your mystrlen function should have the same behavior as the C standard strlen function.

Your functions should not call any of the standard string functions. In the context of this assignment, you should pretend that the standard string functions do not exist.

You should pay special attention to boundary cases. In particular, make sure that your functions work when given empty strings as arguments. For example, make sure that the function call mystrlen("") returns 0.

First create a header file named mystring.h containing the interface to your functions. The interface should consist of a set of function declarations. Then create a file named mystring.cpp containing the implementation of your functions, that is, a set of function definitions. It should "#include" the interface file to insure that each function definition is consistent with its declaration.

You may use array notation to define your functions. For example, this is an acceptable version of the mystrlen function:

int mystrlen(const char pcString[]) { int Length = 0; while (pcString[Length] != '\0') Length++; return Length; }

However we encourage you to use pointer notation instead of array notation to define your functions; pointer notation is used heavily throughout the course, and it would be wise to use this assignment to insure that you are comfortable with it. For example, we encourage you to define your mystrlen function similar to this:

int mystrlen(const char *pcString) { const char *pcStringEnd = pcString; while (*pcStringEnd != '\0') pcStringEnd++; return pcStringEnd - pcString; }

The comment should appear in both the .h file (for the sake of the users of the function) and the .cpp file (for the sake of the implementation of the function).

For example, here is an appropriate way to comment a mystrlen function:

In file mystring.h: ... int mystrlen(const char *pcString); /* Return the length of string pcString. */ ... In file mystring.cpp: ... int mystrlen(const char *pcString) /* Return the length of string pcString. */ { ... } ...

Note that the comment explicitly states what the function returns, and explicitly refers to the function's parameter (pcString).

Test Driver:

#include "mystring.h" #include #include using namespace std; #define MAX_STRING_LENGTH 256 #define ASSURE(b) assure(b, __LINE__) void assure(bool bSuccessful, int iLineNum) { if (! bSuccessful) printf("Test at line %d failed. ", iLineNum); } void testmystrlen(void) { size_t uiResult; uiResult = mystrlen("Ruth"); ASSURE(uiResult == 4); uiResult = mystrlen("Gehrig"); ASSURE(uiResult == 6); uiResult = mystrlen(""); ASSURE(uiResult == 0); } void testmystrcmp(void) { int iResult; iResult = mystrcmp("Ruth", "Ruth"); ASSURE(iResult == 0); iResult = mystrcmp("Gehrig", "Ruth"); ASSURE(iResult < 0); iResult = mystrcmp("Ruth", "Gehrig"); ASSURE(iResult > 0); iResult = mystrcmp("", "Ruth"); ASSURE(iResult < 0); iResult = mystrcmp("Ruth", ""); ASSURE(iResult > 0); iResult = mystrcmp("", ""); ASSURE(iResult == 0); } void testmystrcpy(void)

{ char pc[MAX_STRING_LENGTH]; char *pcResult; pcResult = mystrcpy(pc, "Ruth"); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "Ruth") == 0)); pcResult = mystrcpy(pc, ""); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pc, "") == 0)); } void testmystrcat(void) { char pc[MAX_STRING_LENGTH]; char *pcResult; strcpy(pc, "Ruth"); pcResult = mystrcat(pc, "Gehrig"); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "RuthGehrig") == 0)); strcpy(pc, "Ruth"); pcResult = mystrcat(pc, ""); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "Ruth") == 0)); strcpy(pc, ""); pcResult = mystrcat(pc, "Ruth"); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "Ruth") == 0)); strcpy(pc, ""); pcResult = mystrcat(pc, ""); ASSURE(pcResult != NULL); ASSURE(pcResult == pc); ASSURE((pcResult != NULL) && (strcmp(pcResult, "") == 0)); } void testmystrstr() { char pc[] = "llama"; char pcEmpty[] = ""; char *pcResult; pcResult = mystrstr(pc, "am"); ASSURE(pcResult == pc + 2); pcResult = mystrstr(pc, "m"); ASSURE(pcResult == pc + 3);

pcResult = mystrstr(pc, "ll"); ASSURE(pcResult == pc); pcResult = mystrstr(pc, "l"); ASSURE(pcResult == pc); pcResult = mystrstr(pc, "llama"); ASSURE(pcResult == pc); pcResult = mystrstr(pc, "lamal"); ASSURE(pcResult == NULL); pcResult = mystrstr(pc, ""); ASSURE(pcResult == pc); pcResult = mystrstr(pcEmpty, "llama"); ASSURE(pcResult == NULL); pcResult = mystrstr(pcEmpty, ""); ASSURE(pcResult == pcEmpty); } int main() { testmystrlen(); testmystrcmp(); testmystrcpy(); testmystrcat(); testmystrstr(); printf("If no any error outputs, your program passes all the tests. The End.\ n"); return 0;

Note: please insert one line: #include right between line: #include and line: using namespace std; -I have added them) that you can download and use for some testing of your code. If no error outputs, your program passes all the tests. Otherwise, it will tell you the line numbers of errors in your source codes.

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!