Question

In: Computer Science

Before C++ and classes, strings were stored in simple arrays of characters. The term C-string refers...

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.

Solutions

Expert Solution

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;

}


Related Solutions

You are required to use C++ static or dynamic arrays of characters to store c-strings. You...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You are NOT allowed to use any C++ string data type variable for any purpose. Moreover, you are allowed to add any include directive. You are not allowed to include string, cstdlib or math libraries. Also, you are not allowed to use any built-in functions of c-strings. can someone help with the third, fourth, and fifth functions? I tried many ways but i cannot figure...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You...
You are required to use C++ static or dynamic arrays of characters to store c-strings. You are NOT allowed to use any C++ string data type variable for any purpose. Moreover, you are allowed to add any include directive. You are not allowed to include string, cstdlib or math libraries. Also, you are not allowed to use any built-in functions of c-strings. can someone help with the third, fourth, and fifth functions? I tried many ways but i cannot figure...
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);....
Do not use c-string, arrays, and read input characters one by one. Also use : s.push_back(ch);. Code a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered .The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Eg: Enter a sequence of characters: abAa1121X+&$%$dc[space] Distinct characters are: abA12X+&$%dc. Use C++
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container...
Write a Simple C++ Code to show that the Constructors of Composed Classes Execute before Container Classes.
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
A DNA strand is represented by a string of the characters A, C, G, and T,...
A DNA strand is represented by a string of the characters A, C, G, and T, each of which represents a nucleotide. Each nucleotide has its complement as indicated by this Ruby hash: NUCLEOTIDE_COMPLEMENT = { 'A' => 'T', 'T' => 'A', 'C' => 'G', 'G' => 'C' } The reverse-complement of a DNA string is a new string in which each nucleotide is replaced by its complement and the string is reversed. Reverse-complements are important in bioinformatics since a...
In C++ please modify the following program and add characters (char) and names (strings) to be...
In C++ please modify the following program and add characters (char) and names (strings) to be added to the linked list along with integers. The current demo program accepts only integer data, so you would ask the user to select the data type to be added to the linked list. The user should be given the following three choices: (a) whole numbers (b) single characters (c) strings Once the user makes a selection from the list above then your program...
Given the nested collection that maps each term to a set of strings   Return a string...
Given the nested collection that maps each term to a set of strings   Return a string of terms that are repeated in all the nested sets Given : {apple=[apple BALL carrot, ball !carrot! ,!Dog*&]} {apple=[apple BALL carrot, ball !carrot! ,!Dog*&], dog=[ball !carrot! ,!Dog*&]} Return: [ball !carrot! ,!Dog*&] Public static String common(Map<String, Set<Sting>> map) { }
Write a C program with a struct that contains a string of 12 characters and two...
Write a C program with a struct that contains a string of 12 characters and two integers: number1 and number2. Read all 3 values in from the keyboard using scanf. Print the string and the sum of the two integers using the fully qualified struct names.
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT