#include // C++ Standard Library IO class decls #include // C++ string class decls #include // decls for isalpha() etc. using namespace std; // Example program demonstrating the use of the C++ string class // and some other functions for examining a string object int main(int argc, char* argv[] ) { string aString; // an empty string, initially aString = "hello"; // populates (or repopulates) the string aString += " cs1030"; // concatenates (with +=) to the end // echo the string back to the console cout << "\"" << aString << "\"" << " length is " << aString.length() << endl; string searchString("cs"); // find() searches for the first occurrence of specified searchString within a string // The 2nd parameter is the index in the string where the search begins int index = aString.find_first_of( searchString , 0 ); // returns the index where the substring was found cout << " 1st occurrence " << "\"" << searchString << "\"" << " found at index " << index << endl; if( isalpha( aString[index] ) ) // demonstrates indexing into the string; isalpha() is a ctype library function cout << "char is alpha: " << aString[index] << endl; // iomanip formatting applies! // now find next occurrence index = aString.find_first_of( searchString , index+1 ); // returns the index where the substring was found cout << " 2nd occurrence " << "\"" << searchString << "\"" << " found at index " << index << endl; if( isalpha( aString[index] ) ) // demonstrates indexing into the string; isalpha() is a ctype library function cout << "char is alpha: " << aString[index] << endl; // iomanip formatting applies! return EXIT_SUCCESS; }