Home C String Functions strstr() C strstr() Function Definition In C, strstr() searches a C-string for the first matching substring. strstr("hello world", "world") points at the start of world.
You pass the haystack and the needle. Both are required. The result is a pointer into the haystack, or NULL when the substring is missing. Include <string.h>.
Syntax Pass the text to search, then the text to find. Start with this call.
strstr("hello world", "world"); Call What it does strstr("hello world", "world")Finds the first world inside the sentence.
Include <string.h>. Check for NULL before you use the pointer. The search is case-sensitive.
Library declarations char *strstr(const char *haystack, const char *needle); Parameters Parameter Description haystack Required. The C-string to search. needle Required. The substring to find.
Return Value Return Value Description Pointer Points to the first match inside the haystack. NULL Returned when the substring is not found.
C Examples 1. Find substring 2. Get index 3. Handle NULL Example 1 — Find a Substring Search for "world" inside a sentence and print from the match onward.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *hay = "hello world";
char *p = strstr(hay, "world");
if (p != NULL) {
printf("found: %s\n", p);
}
return 0;
} How It Works Call strstr. It scans hay for the first "world".Get a pointer. On success, p points inside hay at the match.Guard with NULL. Only print when a match exists.Print from the match. %s shows "world" (and anything after it).Example 2 — Get the Match Index Subtract pointers to turn the result into a zero-based index. Both must point into the same array.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *hay = "CodeToFun";
char *p = strstr(hay, "To");
if (p != NULL) {
size_t index = (size_t)(p - hay);
printf("index = %zu\n", index);
printf("from = %s\n", p);
}
return 0;
} How It Works Find the substring. "To" starts at position 4 in "CodeToFun".Subtract pointers. p - hay is the index (bytes from the start).Cast to size_t. Safer for printing unsigned lengths.Reuse p. You still have a pointer to the rest of the string.Example 3 — Handle a Missing Substring When there is no match, strstr returns NULL. Always branch on that result.
#include <stdio.h>
#include <string.h>
int main(void) {
const char *hay = "hello";
char *p = strstr(hay, "xyz");
if (p == NULL) {
printf("not found\n");
} else {
printf("found: %s\n", p);
}
return 0;
} How It Works Search for "xyz". It does not appear in "hello".Get NULL. That is the standard “not found” result.Check first. Never print or dereference p when it is NULL.Tell the user. A clear message avoids crashes and confusion.Frequently Asked Questions What does strstr() do in C? strstr() finds the first matching substring and returns a pointer to it, or NULL.
What are the parameters of strstr()? Both are required. haystack is the text to search, and needle is the substring to find.
What does strstr() return? A pointer to the first match, or NULL when the substring is missing.
Which header file does strstr() need? Include <string.h>.
Is strstr() case-sensitive? Yes. Uppercase and lowercase letters must match.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion