Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

strstr — substring search

strstr from <string.h> finds the first substring and returns a pointer to it, or NULL.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <string.h>
QChar *strstr(QChar *s1, const char *s2);

strstr(s1, s2) finds the first occurrence of s2 in s1, case-sensitively. Empty s2 returns s1.

Both arguments must point to valid \0-terminated strings; NULL is invalid. Check for NULL before using the result. The result points into the original string, not a copy.

In C23, QChar is standard notation, not a type to declare: the result is char * or const char *, depending on s1. The function does not modify data.

Examples

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

int main(void)
{
    const char *text = "red blue red";
    const char *match = strstr(text, "blue");

    if (match != NULL) {
        puts(match);
    }
    return 0;
}

Result

blue red

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources