The basic_string class member, reversed find, rfind() part II program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using rfind() class member function to search a string in a backward direction for the first occurrence of a substring that matches a specified sequence of characters in C++ programming
To show: How to use the reversed find, rfind() part II to search a string in a backward direction for the first occurrence of a substring that matches a specified sequence of characters in C++ programming
// the C++ reversed find, rfind() part II example
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
// searching a string for a substring as specified by a C-string
string str3("Another test. Testing the rfind() the 123");
cout<<"The str3 string is: "<<str3<<endl;
static const basic_string <char>::size_type npos = -1;
basic_string <char>::size_type index5, index6;
const char *cstr3 ="test";
cout<<"Operation: str3.rfind(cstr3)"<<endl;
index5 = str3.rfind(cstr3);
if(index5 != npos)
cout<<"The index of the 1st element of 'test' "
<<"in str3 is: "<<index5<<endl;
else
cout<<"The substring 'test' was not found in str3."<<endl;
const char *cstr4 ="the";
cout<<"\nOperation: str3.rfind(cstr4, index5 + 20, 2)"<<endl;
index6 = str3.rfind(cstr4, index5 + 20, 2);
if(index6 != npos)
cout<<"The index of the next occurrence of 'the' in str3 begins at: " <<index6<<endl;
else
cout<<"There is no next occurrence of 'the' in str3"<<endl;
cout<<endl;
// searching string for a substring as specified by a string
string str4("Final rfind() testing 1...2...3");
cout<<"The str4 string is: "<<str4<<endl;
basic_string <char>::size_type index7, index8;
string str5("2...3");
cout<<"Operation: str4.rfind(str5, 30)"<<endl;
index7 = str4.rfind(str5, 30);
if(index7 != npos)
cout<<"The index of the 1st element of '1...2' "
<<"before\nthe 30th position in str4 is: "<<index7<<endl;
else
cout<<"The substring '1...2' was not found in str4\n"
<<"before the 30th position."<<endl;
string str6("...3");
cout<<"\nOperation: str4.rfind(str6)"<<endl;
index8 = str4.rfind(str6);
if(index8 != npos)
cout<<"The index of the 1st element of '...3' in str4 is: "<<index8<<endl;
else
cout<<"The substring '...3' was not found in str4."<<endl;
return 0;
}
Output example:
The str3 string is: Another test. Testing the rfind() the 123
Operation: str3.rfind(cstr3)
The index of the 1st element of 'test' in str3 is: 8
Operation: str3.rfind(cstr4, index5 + 20, 2)
The index of the next occurrence of 'the' in str3 begins at: 22
The str4 string is: Final rfind() testing 1...2...3
Operation: str4.rfind(str5, 30)
The index of the 1st element of '1...2' before
the 30th position in str4 is: 26
Operation: str4.rfind(str6)
The index of the 1st element of '...3' in str4 is: 27
Press any key to continue . . .