Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
strstr.c File Reference
#include <string.h>
Include dependency graph for strstr.c:

Go to the source code of this file.

Functions

char * strstr (const char *string, const char *substring)
 Finds the first occurrence of the substring in the string. More...
 

Function Documentation

◆ strstr()

char* strstr ( const char *  string,
const char *  substring 
)

Finds the first occurrence of the substring in the string.

Finds the first occurrence of the null-terminated byte string pointed to by substring in the null-terminated byte string pointed to by string. The terminating null characters are not compared.

The behavior is undefined if either stirng or substring is not a pointer to a null-terminated byte string.

Parameters
stringpointer to the null-terminated byte string to examine
substringpointer to the null-terminated byte string to search for
Returns
Pointer to the first character of the found substring in string, or NULL if no such substring is found. If substring points to an empty string, string is returned.

Definition at line 38 of file strstr.c.

39 {
40  const char *a, *b;
41 
42  /* First scan quickly through the two strings looking for a
43  * single-character match. When it's found, then compare the
44  * rest of the substring.
45  */
46 
47  b = substring;
48 
49  if(*b == 0)
50  {
51  return (char*)(uintptr_t)string;
52  }
53 
54  for(; *string != 0; string += 1)
55  {
56  if(*string != *b)
57  {
58  continue;
59  }
60 
61  a = string;
62 
63  while(1)
64  {
65  if(*b == 0)
66  {
67  return (char*)(uintptr_t)string;
68  }
69  if(*a++ != *b++)
70  {
71  break;
72  }
73  }
74 
75  b = substring;
76  }
77 
78  return NULL;
79 }
#define NULL
Definition: stddef.h:15

References NULL.