The C++ find_last_not_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 is not any element of a specified string using C++ find_last_not_of() function
To show: How to use the C++ find_last_not_of() to find through a string for the last character that is not any element of a specified string part I
// the C++ find_last_not_of() part I
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
// searching for a single character in a string
string str1("daddy donkey is dead");
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_not_of('d', 2);
cout<<"Operation: str1.find_last_not_of('d', 2)"<<endl;
if(index1 != npos)
cout<<"The index of the last non 'd' found before the "
<<"2nd position in str1 is: "<<unsigned int(index1)<< endl;
else
cout<<"The non 'd' character was not found."<<endl;
index2 = str1.find_last_not_of('d');
cout<<"\nOperation: str1.find_last_not_of('d')"<<endl;
if(index2 != npos)
cout<<"The index of the non 'd' found in str1 is: "
<<unsigned int(index2)<<endl;
else
cout<<"The Character 'non d' was not found in str1."<<endl;
cout<<endl;
// searching a string for a substring as specified by a C-string
string str2("Testing Testing Testing");
cout<<"str2 string is: "<<str2<<"\n";
basic_string <char>::size_type index3, index4;
const char *cstr ="ei";
index3 = str2.find_last_not_of(cstr, 12);
cout<<"Operation: str2.find_last_not_of(cstr, 12)"<<endl;
if(index3 != npos)
cout<<"The index of the last occurrence of a "
<<"element not of 'ei' in str2 before the 12th "
<<"position is: "<<unsigned int(index3)<<endl;
else
cout<<"Elements not of the substring 'ei' were not "
<<"found in str2 before the 12th position."<<endl;
const char *cstr1 ="g t";
index4 = str2.find_last_not_of(cstr1);
cout<<"\nOperation: str2.find_last_not_of(cstr1)"<<endl;
if(index4 != npos)
cout<<"The index of the last element not "
<<"in 'g t' is: "<<unsigned int(index4)<<endl;
else
cout<<"The elements of the substring 'g t' were "
<<"not found in str2"<<endl;
return 0;
}
Output example:
str1 string is: daddy donkey is dead
Operation: str1.find_last_not_of('d', 2)
The index of the last non 'd' found before the 2nd position in str1 is: 1
Operation: str1.find_last_not_of('d')
The index of the non 'd' found in str1 is: 18
str2 string is: Testing Testing Testing
Operation: str2.find_last_not_of(cstr, 12)
The index of the last occurrence of a element not of 'ei' in str2 before the 12th position is: 11
Operation: str2.find_last_not_of(cstr1)
The index of the last element not in 'g t' is: 21
Press any key to continue . . .