Manipulating Strings - Lab


The Problem

Recall that strings are stored in arrays. Like other kinds of arrays, we can fix a pointer to the beginning of the array:
char label[10] = "value";
char *labelPtr = label;  // Point to the 'label' array.

These would be organized (in memory) as follows:

   label
   ------------------------------------------
+->| v | a | l | u | e | \0 |   |   |   |   |
|  ------------------------------------------
|
|  labelPtr
|  ------
+--+--  |
   ------

Then, either the original array or a pointer to it can be used to access the string's contents:

cout << "label is " << label << endl;
cout << "label is " << labelPtr << endl;

cout << "3rd char is " << label[2] << endl;
cout << "3rd char is " << labelPtr[2] << endl;
The only way to change or compare the content of arrays is to deal with each element. In other words, we can't do the following:

ChangingComparing
char label[10] = "value";
char label2[] = "another";
char *labelPtr = label2;
label = "new val";  // No!
label = label2;     // Wrong!
label = labelPtr;   // Wrong!
char label1[] = "address";
char label2[] = "phone";

if (label1 == label2) // Not what you
                      // think it does!


Aside: We could do the above changes if label was a character pointer (instead of an array); however, what would be happening is the pointer would be taking on the address of a different string, which is not the same as changing the contents of an array.

It would be annoying to have to do things like:

ChangingComparing
char name[10];

name[0] = 'R';
name[1] = 'o';
name[2] = 'b';
name[3] = '\0';
char name1[] = "Dawn";
char name2[] = "Phil";

if (name1[0] == name2[0] &&
    name1[1] == name2[1] &&
    name1[2] == name2[2] &&
    name1[3] == name2[3]) {
  cout << "First 4 chars are same" << endl;
}

or to write loops all the time to do common string operations... Plus, we'd probably forget the nul character (\0) half the time.


string.h

The library string.h has several common functions for dealing with strings. Following are the 4 basic string library functions that we'll discuss:


Your Task

You must write your own versions of functions that accomplish string length, comparing, copying and concatenation, namely:

  1. int StrLength(char *str);
    

    This function should work like strlen() and return the length of a string.

  2. int StrCompare(char *str1, char *str2);
    

    This functions should work like strcmp(), returning a number representing whether the 1st string is greater than, less than, or equal to the 2nd string.

  3. char *StrCopy(char *dest, char *source);
    

    This function should work like strcpy(), copying the contents of the source string into the destination string (and returning the destination string).

  4. char *StrConcat(char *dest, char *source);
    

    This function should work like strcat(), appending the contents of the source string to the end of the destination string (and returning the destination string).


A Test Program

The file strtest.cpp provides a main program to test your string functions.

Compile this test program with your string functions and run it a few times to test it.


BU CAS CS - Manipulating Strings - Lab
Copyright © 1993-2000 by Robert I. Pitts <rip at bu dot edu>. All Rights Reserved.