/* * File name: string-op.cpp * Purpose: To be used in classroom demonstration. * We will use string related functions such as length, substr and * find. * Author: Xiannong Meng * Date: 02-08-2001 */ #include #include using namespace std; void main(void) { string firstName; // two variables of string type string fullName; // need to have #include to use them string myString = "Programming and Problem Solving"; /* first we test the length function */ firstName = "Alexandra"; cout << " The number of characters in first name is : " << firstName.length() << endl; // should be 9 fullName = firstName + ' ' + "Jones"; // concatenate char(s), string(s) cout << " The number of characters in full name is : " << fullName.length() << endl; // should be 15 /* now we test the find function */ /* 1. not found, prints string::npos */ cout << " Position of char 'W' " << fullName.find('W') << endl; /* 2. found a char */ cout << " Position of char 'a' " << fullName.find('a') << endl; /* 3. found a string */ cout << " Position of char 'Jones' " << fullName.find("Jones") << endl; /* now test the substr function */ cout << myString.substr(0,7) << endl; // "Program" cout << myString.substr(7,8) << endl; // "ming and" cout << myString.substr(10,0) << endl; // "" cout << myString.substr(24,40) << endl; // "Solving" cout << myString.substr(40,24) << endl; // run time error }