The C++ basic_string class member reversed find, rfind() function part I 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 the C++ basic_string class member, rfind() 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 search a string in the reverse direction using rfind(), the basic_string class member in C++ programming part I

 

 

 

// the reversed find, rfind() part I C++ example

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// searching for a single character in a string

string str1("Testing the rfind() 1..2..3");

cout<<"str1 string is: "<<str1<<endl;

basic_string <char>::size_type index1, index2;

 

static const basic_string <char>::size_type npos = -1;

cout<<"Operation: str1.rfind('i', 18)"<<endl;

 

index1 = str1.rfind('i', 18);

if(index1 != npos)

cout<<"The index of the 1st 'i' found before\nthe 18th"

<<" position in str1 is: "<<index1<<endl;

else

cout<<"The character 'i' was not found in str1."<<endl;

 

cout<<"\nOperation: str1.rfind('z')"<<endl;

index2 = str1.rfind('z');

if(index2 != npos)

cout<<"The index of the 'z' found in str1 is: "<<index2<<endl;

else

cout<<"The character 'z' was not found in str1."<<endl;

cout<<endl;

 

// searching a string for a substring as specified by C-string

string str2("Testing the rfind() 123");

cout<<"The str2 string is: "<<str2<<endl;

basic_string <char>::size_type index3, index4;

const char *cstr1 ="find";

 

cout<<"Operation: str2.rfind(cstr1, 25)"<<endl;

index3 = str2.rfind(cstr1, 25);

if(index3 != npos)

cout<<"The index of the 1st element of 'find' "

<<"before\nthe 25th position in str2 is: "<<index3<<endl;

else

cout<<"The substring 'find' was not found in str2."<<endl;

 

const char *cstr2 ="nofind()";

cout<<"\nOperation: str2.rfind(cstr2, 25)"<<endl;

index4 = str2.rfind(cstr2, 25);

 

if(index4 != npos)

cout<<"The index of the 1st element of 'nofind()' "

<<"before\n the 25th position in str3 is: "<<index4<<endl;

else

cout<<"The substring 'nofind()' was not found in str2."<<endl;

 

return 0;

}

 

Output example:

 

str1 string is: Testing the rfind() 1..2..3

Operation: str1.rfind('i', 18)

The index of the 1st 'i' found before

the 18th position in str1 is: 14

 

Operation: str1.rfind('z')

The character 'z' was not found in str1.

 

The str2 string is: Testing the rfind() 123

Operation: str2.rfind(cstr1, 25)

The index of the 1st element of 'find' before

the 25th position in str2 is: 13

 

Operation: str2.rfind(cstr2, 25)

The substring 'nofind()' was not found in str2.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index