strstr [C++ Reference]
Keywords: c,string,strstr
Date: 2008-10-14T15:42:03-0700

[[strstr]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## strstr

Syntax:

        #include <cstring>
        char *strstr( const char *str1, const char *str2 );

The function strstr() returns a pointer to the first occurrence of str2 in str1, or NULL if no match is found. If the length of str2 is zero, then strstr () will simply return str1. For example, the following code checks for the existence of one string within another string:

        char* str1 = "this is a string of characters";
        char* str2 = "a string";
        char* result = strstr( str1, str2 );
        if( result == NULL ) printf( "Could not find '%s' in '%s'\n", str2, str1 );
        else printf( "Found a substring: '%s'\n", result );

When run, the above code displays this output:

        Found a substring: 'a string of characters'

Related Topics: memchr, strchr, strcspn, strpbrk, strrchr, strspn, strtok

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •