CIS-255 Home Page: http://www.c-jump.com/bcc/c255c/c255syllabus.htm

Project 2/Part 1: The substring class


Description

The purpose of this assignment is to experiment with std::string, C++ references, and const members of a C++ class. The project focuses on substring operations, similar to text transformations performed by cttl.sourceforge.net open source substring library.

Introduction

A std::string is a C++ Standard Library container designed to accommodate sequence of characters, that is, text. Strings provide built-in features to operate on fragments of text in a more intuitive way than zero-terminated character arrays, commonly known as C-strings. For example,


#include <cassert>
#include <string>
using namespace std;
int main (int argc, char* argv[])
{
    string str( "Hello" );
    for ( int idx = 0; idx < str.length(); ++idx ) {
        // Access and modify individual characters:
        str[ idx ] = toupper( str[ idx ] );
    }
    assert( str == "HELLO" );

    str.append( 3, '!' ); // add three exclamation points
    assert( str == "HELLO!!!" );

    int pos1 = str.find( "L" );  //    HELLO!!!
    int pos2 = str.rfind( "L" ); //      ||
    assert( pos1 == 2 );         // pos1-''-pos2
    assert( pos2 == 3 );

    // Get 2-character long substring starting at pos1:
    assert( str.substr( pos1, 2 ) == "LL" );

    str.replace( pos1, 3, "YDAY" ); // HE...!!!
    assert( str == "HEYDAY!!!" );   //   '-----pos1
    return 0;
}

"Experience shows that it is impossible to design the perfect string. People's taste, expectations, and needs differ too much for that. So, the standard library string isn't ideal. I would have made some design decisions differently, and so would you. However, it serves many needs well, auxiliary functions to serve further needs are easily provided, and std::string is generally known and available. Writing your own string classes has great educational value, but for code meant to be widely used, the standard library string is the one to use." - Bjarne Stroustrup, The C++ Programming Language, Third Edition.

The standard library string provides string manipulation operations such as

However, no general substring facility is provided by the C++ standard, so your assignment is to write a substring C++ class with reasonable support for substrings of std::string.

Instructions

  1. Create new project and add the following C++ source files:

  2. Implementation file substring.cpp includes comments with step-by-step instructions for each member function of the substring class. In this document we outline only general guidelines for the project, in no particular order of importance. For best results, read this assignment doc first. Next, take a look at the main driver to see how substring features can be utilized by the program. Finally, continue on to the comments in substring.cpp file. You need to grasp the big picture of the logical substring before attempting any changes.

  3. NOTE: the source code will not compile as is. However, it will compile as soon as you add initialization list to every constructor of the substring class in substring.cpp file. Try building the original source code, and you should get helpful error messages directing you to the exact places where constructor initialization lists must be added.

  4. The proposed design of the substring class is based on the idea of a half-open range. The first offset refers to the first character of the sequence of characters, and the second offset refers to the position one beyond the last character. Implementation keeps track of absolute positions of the substring in two member variables of the substring class, namely m_begin and m_end. Your first implementation step should be adding these two variables to the definition of the substring class. The substring is considered empty if both positions point to the same offset in the string. If substring is not empty, m_begin stores the position of the first character of the substring, and m_end stores the position of the last character in the substring plus one. For example,

    m_str m_begin m_end Resulting substring
    abc 0 3 abc
    abc 0 1 a
    abc 0 2 ab
    abc 1 3 bc
    abc 3 3 empty

     

  5. All character positions mentioned in code instructions are absolute positions of the offsets from the beginning of the character string. Thus, a logical substring is defined by its positions and entirely dependends on them. A client program is generally responsible for keeping substring objects within the string bounds.

  6. The substring has absolutely no control over the situation when a string is modified outside of the substring class. The substring provides a function named expand( ) to set its beginning and ending positions to the size of the entire string. The same arrangement is implemented by one of the constructor swhen the substring object is instantiated.

  7. The source code is using type size_t for all position computations in the substring class. The size_t is simply an unsigned integer type, suitable for storing absolute offsets of characters inside the std::string container.

  8. Every time you need a new member variable, remember to add it to the initialization list of every constructor. Recall that the initialization lists have to match the exact order of the member variable declarations inside your class.

  9. Copy constructor is automatically generated for you by the compiler. However, if you decide (not part of this assignment) to add your own copy constructor, be sure to take care of all member variables in the initialization list.

  10. IMPORTANT: You must become familiar with basic operations of the std::string STL class. As a general rule, avoid writing your own logic if there are member functions of std::string readily available for the job. Be sure to check the std::string documentation when thinking how to implement substring class functionality.

  11. A good reason for writing a new class is to make it do something really useful. In our case we add character parsing functions to the substring implementation:

    
    #include <iostream>
    #include "substring.h"
    
    int main()
    {
        std::string str( "123abc " );
        substring sub( str );
        if ( sub.parse().isdigit().isalnum().isspace().succeeded() ) {
            sub.match().replace( "xyz" );
        } else {
            sub.unparse(); // restore the original substring
        }
        return 0;
    }
    
    

    A few more notes regarding the above code fragment:

  12. Use your main( ) function to verify and test the functionality of the substring class.

  13. When ready, log into CIS-255 online website, follow the link to Submit Homework, and select Project 2/Part 1 to upload your source files. Alternatively, you may zip the folder with all of your source files and upload a single ZIP archive.

    PLEASE DO NOT send project, EXE, or other MSVC artifacts on your system. I only need your C++ source files to grade the assignment.