Learn to Implement strstr() Function in C

Learn to Implement strstr() Function in C

3 mins read588 Views 3 Comments
Updated on Mar 13, 2024 14:18 IST

Discover how to use the Strstr function in the C programming language, including its syntax, parameters, and practical applications. Explore examples and tips to effectively implement Strstr in your code for searching a specific substring in a given string.

2023_03_strstr-function-in-c.jpg

The strstr() function in C handles strings. Explore this important topic in the C programming world with a few easy examples.

So, without further ado, let’s get started!

Introduction to strstr() Function in C

The strstr() function is a string handling function in the C programming language. It is used to find the first occurrence of a substring (a sequence of characters) within a larger string.

Arguments of strstr()

The strstr() function takes two arguments:

  • A pointer to the original string
  • A pointer to the substring to be searched for

If you are getting stuck, we recommend you take up some C programming courses from top vendors.

Recommended online courses

Best-suited C / C++ courses for you

Learn C / C++ with these high-rated online courses

4.24 K
6 weeks
475
6 weeks
– / –
45 hours
4 K
80 hours
– / –
1 month
– / –
40 hours
– / –
2 months
– / –
1 year
4 K
80 hours

Return Value of strstr()

The function returns a pointer to the first occurrence of the substring in the original string, or a null pointer if the substring is not found.

Syntax of strstr()

The syntax for the strstr() function is:


 
char* strstr(const char* str1, const char* str2);
Copy code
  • str1: A pointer to the string in which to search for the substring.
  • str2: A pointer to the substring to search for in str1.

The strstr() function is case sensitive, meaning that it distinguishes between uppercase and lowercase characters. If you want to perform a case-insensitive search, you will need to use other string handling functions such as strcasestr() (which is not a standard C function).

C Examples of strstr() Function 

Example 1: Finding a substring within a string


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello world!";
char str2[] = "world";
char *result = strstr(str1, str2);
if (result != NULL) {
printf("Substring found at index %d\n", result - str1);
} else {
printf("Substring not found\n");
}
return 0;
}
Copy code

Output

So, we use the strstr() function to find the position of the substring “world” within the string “Hello world!”. The strstr() function returns a pointer to the first occurrence of the substring within the string, or NULL if the substring is not found. We subtract the starting address of the original string str1 from the address returned by strstr() to get the position of the substring. 

Example 2: Case-sensitive search


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "The quick brown fox jumps over the lazy dog";
char str2[] = "brown";
char *result = strstr(str1, str2);
if (result != NULL) {
printf("Substring found at index %d\n", result - str1);
} else {
printf("Substring not found\n");
}
return 0;
}
Copy code

Output

Here, we use the strstr() function to find the position of the substring “brown” within the string “The quick brown fox jumps over the lazy dog”. The strstr() function is case-sensitive. It will not find the substring if spelled differently, such as with a capital letter.

Example 3: Searching for an empty string


 
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello world!";
char str2[] = "";
char *result = strstr(str1, str2);
if (result != NULL) {
printf("Substring found at index %d\n", result - str1);
} else {
printf("Substring not found\n");
}
return 0;
}
Copy code

Output:

In this example, we use the strstr() function to search for an empty string within the string “Hello world!”. Since an empty string is a substring of every string, the strstr() function will always return a pointer to the beginning of the original string in this case.

Note: When using the strstr() function, ensure the strings and the substring being searched for are null-terminated. This means they end with a null character ‘\0‘.

Example 4: Finding multiple occurrences of a substring within a string


 
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello world!";
char sub[] = "wor";
size_t len = strlen(sub);
char *result = str;
while ((result = strstr(result, sub)) != NULL) {
if (strncmp(result, sub, len) == 0) {
printf("Substring found at index %d\n", result - str);
}
result++;
}
return 0;
}
Copy code

Here, we use the strstr() function to find all occurrences of the substring “wor” within the string “Hello world!”. We first calculate the length of the substring using the strlen() function. Then use the strncmp() function to compare the first len characters of each match to the substring we are searching for. This ensures that we only count matches that are exactly len characters long.

Endnotes

In conclusion, the strstr() function in C is a very useful tool for finding substrings within a larger string. This function is a fundamental tool for handling strings in C. It is also a building block for many other string operations. By combining it with string manipulation functions such as strlen() and strncmp(), perform powerful operations on strings in C programs.

Contributed by Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments

(3)

This web page has a wrong title. It is not about implementing strstr (). Instead it is about using strstr ().

Reply to Anonymous

In your implementation above for finding multiple occurrences, why is strncmp () necessary?

Reply to Anonymous

There are two special cases in strstr () that might cause problems with your above implementation of finding multiple occurrences of a substring. The first special case is when the substring (the second parameter to strstr () is zero length. When the substring is of zero length, the string being sea

Reply to Anonymous