Using strstr to search for the first occurrence of a substring - C++ Data Type

C++ examples for Data Type:char array

Description

Using strstr to search for the first occurrence of a substring

Demo Code

#include <iostream> 
#include <cstring> // strstr prototype 
using namespace std; 

int main() //from w ww . j a  va2s .  co m
{ 
    const char *string1 = "abcdefabcdef"; 
    const char *string2 = "def"; 

    cout << "string1 = " << string1 << "\nstring2 = " << string2 
       << "\n\nThe remainder of string1 beginning with the\n" 
       << "first occurrence of string2 is: " 
       << strstr( string1, string2 ) << endl; 
    
    return 0;
}

Result


Related Tutorials