Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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";

Now, the string example[100] contains the following: -------------------------------------------------------------- | F | i | r | s | t | | v | a | l | u | e | \0 | * | * |... -------------------------------------------------------------- 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 the addresses, not the strings!), but it will always evaluate to false. To handle these kinds of difficulties, programmers can rely on the C-string library (also referred to as ). This library, which is part of every proper C++ installation, is an extensive collection of functions for handling C-strings. Following are the 4 basic string library functions that we'll discuss:

(1) strlen(str) Returns the number of characters in the string, not including the null character.

(2) strcmp(str1, str2) This function takes two strings and compares them. If the strings are equal, it returns 0. If the first is greater than the 2nd, then it returns some value greater than 0. If the first is less than the 2nd, then it returns some value less than 0.

You might use strcmp() as in:

#include  char str1[] = "garden"; if (strcmp(str1, "apple") == 0) cout << "Equal" << endl; else cout << "Not equal" << endl;

or

if (strcmp(str1, "eden") > 0) cout << "'" << str1 << "' comes after 'eden'" << endl;

The ordering for strings is lexical order based on the ASCII value of characters. Remember that the ASCII value of 'A' and 'a' (i.e., upper/lowercase) are not the same.

An easy way to remember how to use strcmp() to compare 2 strings (let's say a and b) is to use the following mnemonics: Want... Use... a == b strcmp(a, b) == 0 a < b strcmp(a, b) < 0 a >= b strcmp(a, b) >= 0 ... ...

(3) strcpy(dest, source) Copies the contents of source into dest, as in:

#include  
char str1[10] = "initvalue"; strcpy(str1, "second");
 

Now, the string str1 contains the following: ------------------------------------------- | s | e | c | o | n | d | \0 | u | e | \0 | ------------------------------------------- and the word "initvalue" has been overwritten. Note that it is the first null character (\0) that determines the end of the string. When using strcpy(), make sure the destination is big enough to hold the new string.

Note: An easy way to remember that the destination comes first is because the order is the same as for assignment, e.g: dest = source Also, strcpy() returns the destination string, but that return value is often ignored.

(4) strcat(dest, source) Copies the contents of source onto the end of dest, as in:

#include 
char str2[10] = "first"; strcat(str2, " one");

Now, the string str2 contains the following: ------------------------------------------ | f | i | r | s | t | | o | n | e | \0 | ------------------------------------------ When using strcat(), make sure the destination is big enough to hold the extra characters.

Note: Function strcat() also returns the destination string, but that return value is often ignored.

Assignment Directions

Your task in this assignment is to complete your own versions of these four most 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)

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).

Please right out exactly what I need in full, I am not that familair with programming. Thanks

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions

Question

Project management skills and/or experience desirable

Answered: 1 week ago