Open In App

strstr() in C/C++

In C/C++, std::strstr() is a predefined function used for string matching. <string.h> is the header file required for string functions. This function takes two strings s1 and s2 as arguments and finds the first occurrence of the string s2 in the string s1. The process of matching does not include the terminating null-characters(‘\0’), but function stops there. 

Syntax

char *strstr (const char *s1, const char *s2);

Parameters

Return Value

Example

The below program illustrates the usage of the strstr() function.






// C program to illustrate strstr()
 
#include <stdio.h>
#include <string.h>
 
int main()
{
    // Take any two strings
    char s1[] = "GeeksforGeeks";
    char s2[] = "for";
    char* p;
 
    // Find first occurrence of s2 in s1
    p = strstr(s1, s2);
 
    // Prints the result
    if (p) {
        printf("String found\n");
        printf("First occurrence of string '%s' in '%s' is "
               "'%s'",
               s2, s1, p);
    }
    else
        printf("String not found\n");
 
    return 0;
}




// CPP program to illustrate strstr()
 
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
    // Take any two strings
    char s1[] = "GeeksforGeeks";
    char s2[] = "for";
    char* p;
 
    // Find first occurrence of s2 in s1
    p = strstr(s1, s2);
 
    // Prints the result
    if (p) {
        cout << "String found" << endl;
        cout << "First occurrence of string '" << s2
             << "' in '" << s1 << "' is '" << p << "'"
             << endl;
    }
    else {
        cout << "String not found" << endl;
    }
 
    return 0;
}

Output
String found
First occurrence of string 'for' in 'GeeksforGeeks' is 'forGeeks'

Time Complexity: O(n + m), where n is the size of s1 and m is the size of s2.
Auxiliary Space: O(m), where m is the size of s2.



Note: The official implementation of strstr() is unspecified, its implementation is assumed to consist of any of the standard string matching algorithms. Here, we have assumed that it is implemented using Knuth-Morris-Pratt algorithm, which has time and space complexity as stated above.

Application: Replace a String with Another

In this example with the help of strstr() function we first search for the occurrence of substring “STL” in s1 and after that replace that word with “Strings“.




// CPP program to illustrate strstr()
 
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
    // Take any two strings
    char s1[] = "Fun with STL";
    char s2[] = "STL";
    char* p;
 
    // Find first occurrence of s2 in s1
    p = strstr(s1, s2);
 
    // Prints the result
    if (p) {
        strcpy(p, "Strings");
        cout << s1;
    }
    else {
        cout << "String not found" << endl;
    }
 
    return 0;
}




// C program to illustrate strstr()
 
#include <stdio.h>
#include <string.h>
 
int main()
{
    // Take any two strings
    char s1[] = "Fun with STL";
    char s2[] = "STL";
    char* p;
 
    // Find first occurrence of s2 in s1
    p = strstr(s1, s2);
 
    // Prints the result
    if (p) {
        strcpy(p, "Strings");
        printf("%s", s1);
    }
    else
        printf("String not found\n");
 
    return 0;
}

Output
Fun with Strings

Time Complexity: O(n + m), where n is the size of s1 and m is the size of s2.
Auxiliary Space: O(m), where m is the size of s2.


Article Tags :
C++