strstr - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# strstr

From cppreference.com
< c‎ | string‎ | byte

Defined in header `<string.h>` |  |
char *strstr( const char* str, const char* substr ); |  |

Finds the first occurrence of the byte string `substr` in the byte string pointed to by `str`. The terminating null characters are not compared.

### [edit] Parameters

str | - | pointer to the null-terminated byte string to examine
substr | - | pointer to the null-terminated byte string to search for

### [edit] Return value

Pointer to the first character of the found substring in `str`, or NULL if no such substring is found. If `substr` points to an empty string, `str` is returned.

### [edit] Example

Run this code

    #include <string.h>
    #include <stdio.h>

    void find_str(char const* str, char const* substr)
    {
        char* pos = strstr(str, substr);
        if(pos) {
            printf("found the string '%s' in '%s' at position: %ld\n", substr, str, pos - str);
        } else {
            printf("the string '%s' was not found in '%s'\n", substr, str);
        }
    }

    int main(void)
    {
        char* str = "one two three";
        find_str(str, "two");
        find_str(str, "");
        find_str(str, "nine");
        find_str(str, "n");

        return 0;
    }

Output:

    found the string 'two' in 'one two three' at position: 4
    found the string '' in 'one two three' at position: 0
    the string 'nine' was not found in 'one two three'
    found the string 'n' in 'one two three' at position: 1

### [edit] See also

strchr | finds the first occurrence of a character (function) [edit]
strrchr | finds the last occurrence of a character (function) [edit]
C++ documentation for strstr

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=c/string/byte/strstr&oldid=70099"