cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : C Library : cstring (string.h) : strstr
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cstring (string.h)
functions:
· memchr
· memcmp
· memcpy
· memmove
· memset
· strcat
· strchr
· strcmp
· strcoll
· strcpy
· strcspn
· strerror
· strlen
· strncat
· strncmp
· strncpy
· strpbrk
· strrchr
· strspn
· strstr
· strtok
· strxfrm
macros:
· NULL
types:
· size_t

-

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

Locate substring

Returns a pointer to the first occurrence of str2 in str1, or a null pointer if there str2 is not part of str1.

The matching process does not include the terminating null-characters.

Parameters

str1
C string to be scanned.
str2
C string containing the sequence of characters to match.

Return Value

A pointer to the first occurrence in str1 of any of the entire sequence of characters specified in str2, or a null pointer if the sequence is not present in str1.

Portability

In C, this function is declared as:

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

instead of the two overloaded versions provided in C++.

Example

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  strncpy (pch,"sample",5);
  puts (str);
  return 0;
}
This example searches for the "simple" substring in str and replaces that word for "sample".

Output:


This is a sample string

See also

strspn Get span of character set in string (function)
strpbrk Locate character in string (function)
strchr Locate first occurrence of character in string (function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us