C++ examples for STL:string
Searching for the last occurrence of t or h
#include <iostream> #include <string> using namespace std; void showresult(string s, string::size_type i); int main()/* www .j a v a 2 s . co m*/ { string::size_type indx; // Create a string. string str("one two three, test test one two three test test "); string str2; cout << "String to be searched: " << str << "\n\n"; cout << "Searching for the last occurrence of t or h\n"; indx = str.find_last_of("th"); showresult(str, indx); return 0; } // Display the results of the search. void showresult(string s, string::size_type i) { if(i == string::npos) { cout << "No match found.\n"; return; } cout << "Match found at index " << i << endl; cout << "Remaining string from point of match: " << s.substr(i) << "\n\n"; }