The C++ find_last_of() program example part I

 

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: Searches through a string for the last character that matches any element of a specified string using find_last_of() in C++ programming

To show: How to search through a string for the last character that matches any element of a specified string using find_last_of() in C++ programming part I

 

// the C++ find_last_of() example part I

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// searching for a single character in a string

string str1("Testing 1234 Testing 1234");

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

basic_string <char>::size_type index1, index2;

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

index1 = str1.find_last_of('g', 24);

cout<<"Operation: str1.find_last_of('g', 24)"<<endl;

if(index1 != npos)

cout<<"The index of the last 'g' found before the 24th"

<<" position in str1 is: "<<unsigned int(index1)<<endl;

else

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

 

index2 = str1.find_first_of('z');

cout<<"\nOperation: index2 = str1.find_first_of('z')"<<endl;

if(index2 != npos)

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

<<unsigned int(index2)<<endl;

else

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

cout<<endl;

 

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

string str2("Testing 1234 Testing 1234");

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

basic_string <char>::size_type index3, index4;

const char *cstr = "t1";

index3 = str2.find_last_of(cstr, 25);

cout<<"Operation: str2.find_last_of(cstr, 25)"<<endl;

if(index3 != npos)

cout<<"The index of the last occurrence of an "

<<"element of 't1' in str2 before the 25th "

<<"position is: "<<unsigned int(index3)<<endl;

else

cout<<"Elements of the substring 't1' were not\n"

<<"found in str2 before the 25th position."<<endl;

 

const char *cstr1 = "g3";

index4 = str2.find_last_of(cstr1);

cout<<"\nOperation: str2.find_last_of(cstr1)"<<endl;

if(index4 != npos)

cout<<"The index of the last element of 'g3' "

<<"after the 0th position in str2 is: "

<<unsigned int(index4)<<endl;

else

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

 

return 0;

}

 

Output example:

 

str1 string is: Testing 1234 Testing 1234

Operation: str1.find_last_of('g', 24)

The index of the last 'g' found before the 24th position in str1 is: 19

Operation: index2 = str1.find_first_of('z')

The character 'z' was not found in str1

str2 string is: Testing 1234 Testing 1234

Operation: str2.find_last_of(cstr, 25)

The index of the last occurrence of an element of 't1' in str2 before the 25th position is: 21

Operation: str2.find_last_of(cstr1)

The index of the last element of 'g3' after the 0th position in str2 is: 23

Press any key to continue . . .

 

 

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