In this article, we’ll take a look at using the strstr() function in C / C++.
The strstr() function is very useful if you want to find out whether one string is a sub-string of another. It also gives us the pointer to the first match!
Due to this convenience, strstr()
is used somewhat frequently in C, since it is a part of the C standard.
Table of Contents
Basic Syntax of strstr() in C / C++
This function takes in two input strings search_string
and target
. Here, we will search the string search_string
for the target pattern target
.
NOTE: This function ignores the terminal ‘\0’ for both the strings
It returns a char*
pointer to the first character of the first match, if found.
If target
is not a sub-string of search_string
, the returned value is NULL
.
Also, it is defined in the <string.h>
header file, so make sure that you include this header file first!
#include <string.h> char* strstr(const char* search_string, const char* target);
Now, this description must be pretty clear to you. Let’s now look at some examples, to exactly see what happens.
Using strstr() in C / C++ – Some Examples
Let’s take a simple string “Hello from JournalDev”, and search for “JournalDev”. If this function works as expected, we’ll get a pointer to the first match character (“J”).
Let’s write our program first.
#include <stdio.h> #include <string.h> int main() { char* search_string = "Hello from JournalDev"; char* target = "JournalDev"; printf("Search String: %s\n", search_string); printf("Target Pattern: %s\n", target); char* result = strstr(search_string, target); if (result == NULL) { printf("Target pattern is not a substring!\n"); } else { printf("Target pattern is a substring!\n"); printf("First character of result = %c\n", *result); printf("The complete result string (result) = %s\n", result); } return 0; }
Output
Search String: Hello from JournalDev Target Pattern: JournalDev Target pattern is a substring! First character of result = J The complete result string (result) = JournalDev
Indeed, we get what we expected! strstr()
finds out that “JournalDev” is a substring of “Hello from JournalDev”, and returns a pointer to the first match (“J”).
From there, we can simply print the rest of the string, which is a sub-string of the original (“JournalDev”)!
Now, let’s take another string, which is not a sub-string of the input.
#include <stdio.h> #include <string.h> int main() { char* search_string = "Hello from JournalDev"; char* target = "Hi JournalDev"; printf("Search String: %s\n", search_string); printf("Target Pattern: %s\n", target); char* result = strstr(search_string, target); if (result == NULL) { printf("Target pattern is not a substring!\n"); } else { printf("Target pattern is a substring!\n"); printf("First character of result = %c\n", *result); printf("The complete result string (result) = %s\n", result); } return 0; }
Output
Search String: Hello from JournalDev Target Pattern: Hi JournalDev Target pattern is not a substring!
Since our target pattern is not a sub-string of the input, result
will be NULL
!
Hopefully, these examples make it pretty clear about what strstr()
does.
Conclusion
In this article, we learned about using the strstr() function in C / C++. This function makes it easy to check if a string is a sub-string of another string, and also gives a pointer to the first match, if it exists!
References
- The Linux manual page documentation on strstr() in C