Open In App
Related Articles

strstr() in C/C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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

  • s1: This is the main string to be examined.
  • s2: This is the sub-string to be searched in string.

Return Value

  • This function returns a pointer point to the first character of the found s2 in s1 otherwise a null pointer if s2 is not present in s1.
  • If s2 points to an empty string, s1 is returned.

Example

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

C

// 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;
}

                    

C++

// 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“.

C++

// 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

// 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.



Last Updated : 21 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments