Searching for the first occurrence of t or h - C++ STL

C++ examples for STL:string

Description

Searching for the first occurrence of t or h

Demo Code

#include <iostream>
#include <string>
using namespace std;
void showresult(string s, string::size_type i);
int main()/*from w ww. j ava2s.c  o  m*/
{
   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 t or h\n";
   indx = str.find_first_of("th");
   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