The C++ find() member function 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: Using the C++ find() member function to find 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 I
// the C++ find() program example part I
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
// don't forget the null character
// searching for a single character in a string
string str1("Search part I, a character in a string");
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('r', 2);
cout<<"Operation: str1.find('r', 2)"<<endl;
if(index1 != npos)
cout<<"The index of the 1st 'r' found after the 2nd"
<<" position in str1 is: "<<unsigned int(index1)<<endl;
else
cout<<"The character 'r' was not found in str1."<<endl;
cout<<endl;
index2 = str1.find('t');
cout<<"Operation: str1.find('t')"<<endl;
if(index2 != npos)
cout<<"The index of the 't' found in str1 is: "<<unsigned int(index2)<<endl;
else
cout<<"The character 't' was not found in str1."<<endl;
cout<<endl;
// searching a string for a substring as specified by a C-string
string str2("Search part II, a substring in string");
cout<<"str2 string is: "<<str2<<endl;
basic_string <char>::size_type index3, index4;
const char *cstr1 ="sub";
index3 = str2.find(cstr1, 5);
cout<<"Operation: str2.find(cstr1, 5)"<<endl;
if(index3 != npos)
cout<<"The index of the 1st element of 'sub' after the 5th "
<<"position in str2 is: "<<unsigned int(index3)<<endl;
else
cout<<"The substring 'sub' was not found in str2"<<endl;
cout<<endl;
const char *cstr2 ="bstring";
index4 = str2.find(cstr2, 0);
cout<<"Operation: str2.find(cstr2, 0)"<<endl;
if(index4 != npos)
cout<<"The index of the 1st element of 'bstring' "
<<"after the 0th position in str2 is: "<<unsigned int(index4)<<endl;
else
cout<<"The substring 'bstring' was not found in str2"<<endl;
return 0;
}
Output example:
str1 string is: Search part I, a character in a string
Operation: str1.find('r', 2)
The index of the 1st 'r' found after the 2nd position in str1 is: 3
Operation: str1.find('t')
The index of the 't' found in str1 is: 10
str2 string is: Search part II, a substring in string
Operation: str2.find(cstr1, 5)
The index of the 1st element of 'sub' after the 5th position in str2 is: 18
Operation: str2.find(cstr2, 0)
The index of the 1st element of 'bstring' after the 0th position in str2 is: 20
Press any key to continue . . .