Demonstration of some of the string functions in the Standard C++
string class
//---------------------------------------------------------------
// File: CPPStringDemo.cpp
// Purpose: Demonstration of string functions available through
// the string string class in C++.
// Programming Language: C++
// Author: Dr. Rick Coleman
//---------------------------------------------------------------
#include <iostream> // Note: This program won't work if you use <iostream.h>
#include <string>
using std::string;
using std::cout;
using std::cin;
int main(int argc, char **argv)
{
string line; // Declare a string for demo purposes
// Note the addition of "std::" to get the
// definition of string from the standard (std)
// namespace.
const char *cStr; // Used to demonstrate c_str() function. See below
char cArray[128];
cout << "======================================================================\n";
cout << "Demonstrating copying the string \"This is a test string.\" into the\n";
cout << " instance of the string class str. This demonstration uses the\n";
cout << " overloaded equals operator (=)\n";
cout << "----------------------------------------------------------------------\n\n";
// Copy the string using the overloaded operator=
line = "This is a test string.";
// Print the results
cout << "line: " << line << "\n";
// Also print the length of the string using strlen()
cout << "Using line.length() Length = " << line.length() << "\n";
cout << "Using line.size() Length = " << line.size() << "\n";
cout << "\n\n++++++++++ Press [Enter] to continue. ++++++++++\n\n";
cin.get();
cout << "----------------------------------------------------------------------\n\n\n\n";
cout << "======================================================================\n";
cout << "Demonstrating concatenating the string \" It uses string functions.\"\n";
cout << " onto the string line. This demonstration uses the overloaded\n";
cout << " plus-equals operator (+=)\n";
cout << "----------------------------------------------------------------------\n\n";
// Copy the string using strcpy
line += " It uses string functions.";
// Print the results
cout << "line: " << line << "\n";
// Print the new length
cout << "Length = " << line.length() << "\n";
cout << "\n\n++++++++++ Press [Enter] to continue. ++++++++++\n\n";
cin.get();
cout << "----------------------------------------------------------------------\n\n\n\n";
cout << "======================================================================\n";
cout << "Demonstrating comparing the string using the overloaded operators\n";
cout << " operator== (equals), operator< (does string come alphabetically before)\n";
cout << " and operator> (does string come alphabetically after)\n";
cout << "----------------------------------------------------------------------\n\n";
cout << "Copying \"MNOP\" into line\n\n";
line = "MNOP";
// Compare line to 3 other strings with the == operator.
// One string comes alphabetically before, one is equal, and one comes
// alphabetically after
cout << "Comparing \"MNOP\" to the string \"ABCD\" with (line == \"ABCD\"). ";
if(line == "ABCD")
cout << "\tresults = true\n";
else
cout << "\tresults = false\n";
cout << "Comparing \"MNOP\" to the string \"MNOP\" with (line == \"MNOP\"). ";
if(line == "MNOP")
cout << "\tresults = true\n";
else
cout << "\tresults = false\n";
cout << "Comparing \"MNOP\" to the string \"WXYZ\" with (line == \"WXYZ\"). ";
if(line == "WXYZ")
cout << "\tresults = true\n\n";
else
cout << "\tresults = false\n\n";
// Compare line to 3 other strings with the > operator.
// One string comes alphabetically before, one is equal, and one comes alphabetically after
cout << "Comparing \"MNOP\" to the string \"ABCD\" with (line > \"ABCD\"). ";
if(line > "ABCD")
cout << "\tresults = true\n";
else
cout << "\tresults = false\n";
cout << "Comparing \"MNOP\" to the string \"MNOP\" with (line > \"MNOP\"). ";
if(line > "MNOP")
cout << "\tresults = true\n";
else
cout << "\tresults = false\n";
cout << "Comparing \"MNOP\" to the string \"WXYZ\" with (line > \"WXYZ\"). ";
if(line > "WXYZ")
cout << "\tresults = true\n\n";
else
cout << "\tresults = false\n\n";
// Compare line to 3 other strings with the < operator.
// One string comes alphabetically before, one is equal, and one comes alphabetically after
cout << "Comparing \"MNOP\" to the string \"ABCD\" with (line < \"ABCD\"). ";
if(line < "ABCD")
cout << "\tresults = true\n";
else
cout << "\tresults = false\n";
cout << "Comparing \"MNOP\" to the string \"MNOP\" with (line < \"MNOP\"). ";
if(line < "MNOP")
cout << "\tresults = true\n";
else
cout << "\tresults = false\n";
cout << "Comparing \"MNOP\" to the string \"WXYZ\" with (line < \"WXYZ\"). ";
if(line < "WXYZ")
cout << "\tresults = true\n\n";
else
cout << "\tresults = false\n\n";
cout << "\n\n++++++++++ Press [Enter] to continue. ++++++++++\n\n";
cin.get();
cout << "----------------------------------------------------------------------\n\n\n\n";
cout << "======================================================================\n";
cout << "\n\nDemonstrating other string functions\n";
cout << "----------------------------------------------------------------------\n\n";
cout << "Copying the string \"How do I program thee. Let me iterate the ways.\"";
cout << " into line.\n\n";
line = "How do I program thee. Let me iterate the ways.";
// These have an oops! Finds first occurance of any of the letters in the string
cout << "\n\n";
cout << "======================================================================\n";
cout << "Demonstrating the function find() used to find the first occurance of\n";
cout << " a substring in a given string. This function returns the index in\n";
cout << " the string where the search string was found, or -1 if not found.\n";
cout << "----------------------------------------------------------------------\n\n";
cout << "Results of call to line.(\"I\") = " << line.find("I") <<
"\n";
cout << "To print the string starting from that point use:\n";
cout << "\t cout << line.substr(line.find(\"I\"), line.length());\n\n";
cout << "\t Results = " << line.substr(line.find("I"), line.length()) <<
"\n\n";
cout << "Results of call to line.find(\"the\") = " <<
line.find("the") << "\n";
cout << "To print the string starting from that point use:\n";
cout << "\t cout << line.substr(line.find(\"the\"), line.length());\n\n";
cout << "\t Results = " << line.substr(line.find("the"), line.length()) <<
"\n\n";
int pos = line.find("love");
cout << "Results of call to line.find(\"love\") = " << pos << "\n";
// FYI: If you try to print thr results of the call to find when searching for a
// string that is not found cout will print the returned index as an unsigned
// int. That means instead of printing the correct -1 it will print 4294967295
cout << "\n\n++++++++++ Press [Enter] to continue. ++++++++++\n\n";
cin.get();
cout << "----------------------------------------------------------------------\n\n\n\n";
cout << "\n\n";
cout << "======================================================================\n";
cout << "Demonstrating the function rfind() used to find the last occurance of\n";
cout << " a substring in a given string. This function returns the index in\n";
cout << " the string where the search string was found, or -1 if not found.\n";
cout << "----------------------------------------------------------------------\n\n";
cout << "Results of call to line.rfind(\"L\") = " << line.rfind("L") <<
"\n";
cout << "To print the string starting from that point use:\n";
cout << "\t cout << line.substr(line.rfind(\"L\"), line.length());\n\n";
cout << "\t Results = " << line.substr(line.rfind("L"), line.length()) << "\n\n";
cout << "Results of call to line.rfind(\"the\") = " <<
line.rfind("the") << "\n";
cout << "To print the string starting from that point use:\n";
cout << "\t cout << line.substr(line.rfind(\"the\"), line.length());\n\n";
cout << "\t Results = " << line.substr(line.rfind("the"), line.length()) <<
"\n\n";
pos = line.rfind("love");
cout << "Results of call to line.rfind(\"love\") = " << pos << "\n";
// FYI: If you try to print thr results of the call to find when searching for a
// string that is not found cout will print the returned index as an unsigned
// int. That means instead of printing the correct -1 it will print 4294967295
cout << "\n\n++++++++++ Press [Enter] to continue. ++++++++++\n\n";
cin.get();
cout << "----------------------------------------------------------------------\n\n\n\n";
cout << "======================================================================\n";
cout << "\n\nDemonstrating the copying of the contents of a string class \n";
cout << "into a char array using the command cStr = line.c_str().\n";
cout << "The variable cStr must be declared as const char *cStr; Memory is \n";
cout << "dynamically allocated for the new character and the pointer set point to it.\n";
cout << "Warning: potential memory leak if you set cStr pointing to something else.\n\n";
cout << "----------------------------------------------------------------------\n\n";
cStr = line.c_str();
cout << "const char *cStr = " << cStr << "\n\n";
cout << "\n\n++++++++++ Press [Enter] to continue. ++++++++++\n\n";
cin.get();
cout << "----------------------------------------------------------------------\n\n\n\n";
cout << "======================================================================\n";
cout << "\n\nDemonstrating the copying of the contents of a string class \n";
cout << "into a char array using the command line.copy(cArray, line.length()).\n";
cout << "The variable cArray is declared as char cArray[128];\n";
cout << "Warning: This copy function DOES NOT ADD THE NULL TERMINATOR.\n";
cout << "To use cArray as a C style string you must add the null terminator\n";
cout << "like this: cArray[line.length()] = '\\0';\n\n";
cout << "----------------------------------------------------------------------\n\n";
line.copy(cArray, line.length());
cArray[line.length()] = '\0';
cout << "Char array cArray = " << cArray << "\n\n";
cout << "\n\n++++++++++ Press [Enter] to continue. ++++++++++\n\n";
cin.get();
cout << "----------------------------------------------------------------------\n\n\n\n";
return 0;
}