The Cpp String Data Type

Introduction

This tutorial introduces basic techniques for manipulating the String data type in C Plus Plus (C++). We address the concept of a representing a string, accessing individual character elements in the string, and outputting the string to stdout.

The original language did not include a native type for representing strings. Programmers adapted by implementing an null-terminated character array that was referred to in programming circles as a string. This type of string was supported by ANSI standard functions found in the ANSI standard header string.h.

C Plus Plus (C++) includes a class called String that provides more functionality and more data integrity that the original C string. The C++ string is defined in the header called string.

The C Plus Plus String Type

/**********************************************************************
 * The C Plus Plus String Type                               main.cpp *
 * Author: nicomp                                                     *
 * Abstract: This project illustrates techniques for C++ strings.     *
 *  This data type abstracts the concept of a 'string' and            *
 *  eliminates the need for all that tedious mucking about in         *
 *  pointers. It's considered a replacement for null-terminated       *
 *  arrays of characters.                                             *
 * Note: the string data type is implemented as a C++ class.          *
 *                                                                    *
**********************************************************************/
#include <iostream>
#include <vector>
#include <String>    // This is the C++ String Type

using namespace std;

void main()
{
//    One string          012345678901
      string strBuff = "Hello World";
    
//    You can index it just like an array.
//    Note the single quotes for a single character.
      strBuff[0] = 'J';    // Now it contains "Jello World"

//    Unlike a char array, 
//     you can't use an index that's beyond the current size.
//    strBuff[12] = '!';        // oops. A runtime error.

//    You can dynamically resize the variable, unlike a char array.
      strBuff = "Indiana Hoosiers";

//    You can print the contents.
      cout << "\n The string contains >" << strBuff.c_str() << "<";

//    You can check the length by calling a function.
      cout << "\n The string is " << strBuff.size() << 
           " characters long.";

//    You can append more characters to the end.
      strBuff.append("!");
      cout << "\n After appending, the string contains >" 
      << strBuff.c_str() << "<";

//    You can insert characters at the beginning.
      strBuff.insert(0, "Your ");
      cout << "\n After inserting, the string contains >" 
      << strBuff.c_str() << "<";

//    Check the length again.
      cout << "\n The string is now " << strBuff.size() <<  
          " characters long.";

//    ***********************
//    Variations on a theme
//    ***********************

//    An array of strings
      string strStringArray[5];

//    A vector of strings
      vector<string> strStringVector;

//    An array of string vectors
      vector<string> strStringVectorArray[5];

      cout << "\n\n\n";
}

More by this Author


Comments 1 comment

nicomp profile image

nicomp 6 years ago from Ohio, USA Author

@ki: Thanks for stopping by.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working