Searching for the first occurrence of - C++ STL

C++ examples for STL:string

Description

Searching for the first occurrence of

Demo Code

#include <iostream>
#include <string>
using namespace std;
void showresult(string s, string::size_type i);
int main()// w w w  .  ja  v  a  2s. c  om
{
   string::size_type indx;
   // Create a string.
   string str("one two three, test test one two three test test ");
   string str2;
   cout << "String to be searched: " << str << "\n\n";
   cout << "Searching for the first occurrence of 'two'\n";
   indx = str.find("two");
   showresult(str, indx);
   return 0;
}
// Display the results of the search.
void showresult(string s, string::size_type i) {
   if(i == string::npos) {
      cout << "No match found.\n";
      return;
   }
   cout << "Match found at index " << i << endl;
   cout << "Remaining string from point of match: " << s.substr(i) << "\n\n";
}

Result


Related Tutorials