The C++ find() member function code  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: Using the C++ find() member function to search a string in a forward direction for the first occurrence of a substring that matches a specified sequence of characters

To show: How to use the C++ find() member function to search a string in a forward direction for the first occurrence of a substring that matches a specified sequence of characters part II

 

// the C++ find() code example part II

#include <string>

#include <iostream>

using namespace std;

 

int main(void)

{

// don't forget the null character

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

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

string str3("Again, search part III");

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

basic_string <char>::size_type index5, index6;

const char *cstr3 = "part";

index5 = str3.find(cstr3);

 

cout<<"Operation: str3.find(cstr3)"<<endl;

if(index5 != npos)

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

<<"in str3 is: "<<unsigned int(index5)<<endl;

else

cout<<"The substring 'part' was not found in str3"<<endl;

cout<<endl;

const char *cstr4 = "ar";

index6 = str3.find(cstr4, index5 + 1, 2);

 

cout<<"Operation: str3.find(cstr4, index5 + 1, 2)"<<endl;

if(index6 != npos)

cout<<"The index of the next occurrence of 'ar' in "

<<"str3 begins at: "<<unsigned int(index6)<<endl;

else

cout<<"There is no next occurrence of 'ar' in str3."<<endl;

cout<<endl;

 

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

string str4("Finally!, search part IV");

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

basic_string <char>::size_type index7, index8;

string str5("part");

index7 = str4.find(str5, 4);

 

cout<<"Operation: str4.find(str5, 4)"<<endl;

if(index7 != npos)

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

<<"after the 4th position in str4 is: "<<unsigned int(index7)<<endl;

else

cout<<"The substring 'part' was not found in str4"<<endl;

cout<<endl;

string str6("arch");

index8 = str4.find(str6);

 

cout<<"Operation: str4.find(str6)"<<endl;

if(index8 != npos)

cout<<"The index of the 1st element of 'arch' in "

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

else

cout<<"The substring 'arch' was not found in str4"<<endl;

 

return 0;

}

 

Output example:

 

str3 string is: Again, search part III

Operation: str3.find(cstr3)

The index of the 1st element of 'part' in str3 is: 14

 

Operation: str3.find(cstr4, index5 + 1, 2)

The index of the next occurrence of 'ar' in str3 begins at: 15

 

str4 string is: Finally!, search part IV

Operation: str4.find(str5, 4)

The index of the 1st element of 'part' after the 4th position in str4 is: 17

 

Operation: str4.find(str6)

The index of the 1st element of 'arch' in str4 is: 12

Press any key to continue . . .

 

 

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