The C++ find_last_not_of() program  example part II

 

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 is not any element of a specified string using find_last_not_of() in C++ programming

To show: How to using find_last_not_of() to search through a string for the last character that is not any element of a specified string in C++ programming part II

 

// the C++ find_last_not_of() example part II

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

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

string str3("Playing Testing Boring");

cout<<"str3 string is: "<<str3<<"\n";

basic_string <char>::size_type index5, index6;

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

const char *cstr2 = "PTB";

index5 = str3.find_last_not_of(cstr2);

cout<<"Operation: str3.find_last_not_of(cstr2)"<<endl;

if(index5 != npos)

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

<<"element in str3 other than one of the "

<<"characters in 'PTB' is: "<<unsigned int(index5)<<endl;

else

cout<<"Elements in str3 contain only characters in the string 'PTB'"<<endl;

const char *cstr3 = "gTB";

index6 = str3.find_last_not_of(cstr3, 6, index5-1);

cout<<"\nOperation: str3.find_last_not_of(cstr3, 6, index5-1)"<<endl;

if(index6 != npos)

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

<<"element not in 'gTB' in str3 is: "

<<unsigned int(index6)<<endl;

else

cout<<"Elements in str3 contains only characters "

<<"in the string 'gTB'."<<endl;

cout<<endl;

 

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

string str4("Testing 123 Testing 123");

cout<<"str4 string is: "<<str4<<"\n";

basic_string <char>::size_type index7, index8;

string str5("3 1");

index7 = str4.find_last_not_of(str5, 18);

cout<<"Operation: str4.find_last_not_of(str5, 18)"<<endl;

if(index7 != npos)

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

<<"element not in '3 1' in str4 before the 18th "

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

else

cout<<"Elements other than those in the substring"

<<" '3 1' were not found in the string str4"<<endl;

string str6("Testing");

index8 = str4.find_last_not_of(str6);

cout<<"\nOperation: str4.find_last_not_of(str6)"<<endl;

if(index8 != npos)

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

<<"element not in 'Testing' in str4 before the end "

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

else

cout<<"Elements other than those in the substring\n"

<<"'Testing' were not found in the string str4"<<endl;

 

return 0;

}

 

Output example:

 

str3 string is: Playing Testing Boring

Operation: str3.find_last_not_of(cstr2)

The index of the last occurrence of an element in str3 other than one of the characters in 'PTB' is: 21

 

Operation: str3.find_last_not_of(cstr3, 6, index5-1)

The index of the occurrence of an element not in 'gTB' in str3 is: 3

 

str4 string is: Testing 123 Testing 123

Operation: str4.find_last_not_of(str5, 18)

The index of the last occurrence of an element not in '3 1' in str4 before the 18th position is: 18

 

Operation: str4.find_last_not_of(str6)

The index of the last occurrence of an element not in 'Testing' in str4 before the end position is: 22

Press any key to continue . . .

 

 

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