In: Computer Science
Before C++ and classes, strings were stored in simple arrays of characters. The term C-string refers to the classic implementation of strings in the C programming language. A C-string is a sequence of characters terminated by the null character, '\0'. Since C is part of C++, C-string implementations are valid in C++ as well as C. Recall that an array of characters is the underlying data structure for storing C-strings. For example, this definition creates such an array.
char myString[100];
myString will be capable of holding a C-string with up to 99 characters before the terminating null character. Of course, myString can hold a shorter C-string, too. For example, these assignments give myString the value "xyz".
myString[0] = 'x';
myString[1] = 'y';
myString[2] = 'z';
myString[3] = '\0';
It is legal to initialize a string variable, like this.
char example[100] = "First value";
However, it is not legal to assign string variables, because you cannot assign to an entire array.
myString = "xyz"; // ILLEGAL: Cannot assign to an array
Furthermore, you cannot do comparisons like this.
if (myString == "xyz")
cout << "fantasy land" << endl;
The comparison myString == "xyz" is actually legal (it will compile, syntactically correct), but it will always evaluate to false (semantically incorrect). To handle these kinds of difficulties, programmers can rely on the C-string library. This library, which is part of every proper C++ installation, is an extensive collection of functions for handling C-strings.
Your Task
Your task in this assignment is to create your versions of the most commonly used standard string functions. Specifically, you should create these functions:
Your version (use these for Lab3Proj3.h) |
ISO C standard string functions |
Specifications |
int mystrlen( const char *s) |
int strlen( const char *s) |
Determines the length of the string s. Returns the number of characters in the string before the '\0'. |
int mystrcmp( const char *s1, const char *s2) |
int strcmp( const char *s1, const char *s2) |
Compares the string s1 to the string s2. The function returns 0 if they are the s |
char *mystrcpy( char *s1, const char *s2) |
char *strcpy( char *s1, const char *s2) |
Copies the string s2 into the character array s1. The value of s1 is returned. |
char *mystrcat( char *s1, const char *s2) |
char *strcat( char *s1, const char *s2) |
Appends the string s2 to the end of character array s1. The first character from s2 overwrites the '\0' of s1. The value of s1 is returned |
Each of your functions should have the same behavior as the corresponding ISO C standard string function. For example, your mystrlen() function should have the same behavior as the ISO 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.
You need to create a header file named Lab3Proj3.h containing the implementation of the four functions, that is, a set of function definitions. Your Lab3Proj3.cpp file must also #include this header.
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 )
/* Return the length of string pcString. */
{
const char *pcStringEnd = pcString;
while ( *pcStringEnd != '\0' )
pcStringEnd++;
return pcStringEnd - pcString;
}
The comment should appear in the function as above. Note that the comment explicitly states what the function returns, and explicitly refers to the function's parameter (pcString).
We provide a driver file Project3driverWindow.cpp that you can download and use for testing 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.
If you have any doubts, please give me comment...
int mystrlen(const char *s){
int length = 0;
while(*(s+length)!='\0')
length++;
return length;
}
int mystrcmp(const char *s1, const char *s2){
int len1 = mystrlen(s1), len2 = mystrlen(s2);
int i=0, sum = 0;
while(i<len1 && i<len2){
if(*(s1+i)<*(s2+i))
return -1;
else if(*(s1+i)>*(s2+i))
return 1;
i++;
}
if(i==len1 && i==len2)
return 0;
else if(len1<len2)
return -1;
else
return 1;
}
char *mystrcpy(char *s1, const char *s2){
int i=0;
while(*(s2+i)!='\0'){
*(s1+i) = *(s2+i);
i++;
}
*(s1+i) = '\0';
return s1;
}
char *mystrcat(char *s1, const char *s2){
int len = mystrlen(s1);
int i=0;
while(*(s2+i)!='\0'){
*(s1+len+i) = *(s2+i);
i++;
}
*(s1+len+i) = '\0';
return s1;
}