The strstr() function is a built-in function used to process strings that are handled by library string.h. It is the library that provides us multiple functions to manipulate string. strstr() is used to find the specified main string’s first appearance of the matched substring by searching the source string. If the search is successful, it will return a pointer to the first substance of a substring in a string to be searched. In case it is appearing for more than once, it will pass the pointer to the first appearance of the substring. If the desired substring doesn’t appear, it will return null /0.
Char*strstr() is responsible for handling character datatype for our found string and will also return the string. In this function two parameters are passed: one will be the string to be fetched and the second one from which the string will be fetched, or we can say it is the main string.
In the syntax above, we passed two constant character strings “string_a” and “string_b”. string_a is the one that will be searched, whereas string_b is the main string from which the pointer will search for string_a.
How it works
To perform a search in a string, we first define two variables: the main string and the substring as we can see in the figure below. The second one will be our substring that we have to search for in the main string.
The pointer moves to the first appearance of the substring within the main string as it appears first. It will display the searched string. This method is useful for searching one string in a huge paragraph.
In the example below, we are going to use the strstr() function to find the desired string within the string. To accomplish this, we will first include our header files that are “stdio.h” and “string.h”. “stdio.h” stands for standard input output library, which is used to perform input-output operations. “string.h” is the header file that defines multiple functions to manipulate strings and arrays.
After including our header files, we will proceed to the main() function that is the core part of our program. The main() function is the one in which our complete operations, that will be executed, are performed. In our main function we will declare two-character arrays “string_a” and “string_b”. string_a of array size 100 is responsible for storing the main string from which the substring will be searched. The second variable “string_b” is the one in which we will store the string that is being searched from “string_a”.
After the declaration of variables, we will assign the “string_b” and strstr() function in which we are passing two parameters: the first one is the main string “string_a” and the second one is the string “strstr” that is to be searched. Next to it, we will print our output using the printf() statement.
As a result, the output will be displayed like the snippet below.
In this instance, we are going to search substring using an if-else statement. Proceeding to our code, we will first define our header files “string.h” and “stdio.h”. The purpose of these header files is discussed in the example above. After including header files, we will move to our main() function where we will first initialize our character variables using an array because we have to store more than a single character so we are using arrays.
We initialized three-character variables named “string_a” , “string-b” and “string_f”. String_a is responsible to hold the main string, while string_b will hold the substring that we are going to fetch. string_f is used to hold the strstr() functions return value. Next, we assigned the strstr() function to the variable “string_f”. We passed two variables main string variable “string_a” and the substring holding variable “string_b”.
In the next step, we will define the if() statement to which we passed “string_f” as a parameter. If the passed variable is true and it has found the desired substring out of the main string, it will move to the inner step of if() statement where we will be printing our statement as an output. Here, “%s” is responsible for printing the string. As we first passed the variable “string_b” that holds “second strstr”, it is displayed as first “%s”. Then, the “string_a” value will be displayed in the second appearance of “%s” as it will be performed at the third appearance. If the statement is not true, it will simply display a ” string not found message”
As a result, our output is successfully executed as shown in the screenshot below.
In this code, we will be displaying the else statements output. Like the above output we displayed the if() statement as true. But in this, we are going to display it as false and will execute the else statement.
To do so, we repeated the whole procedure as was in “Example 2”. First, we declare our header files and initialize our variables. The only thing we changed in this code is the substring that we are going to search for in the main string. In this case, our pointer will first check for the if statement whether it is true or not. If it is true, it will print the if() statements output. While in our case our if() statement is not true so it will print the else statement that is “string not found” as shown in the figure below.
In this guide, we have learned about the strncpy() function of the C language. You can explore more about the strncpy() function of the C library by using multiple examples, you can gain an even better understanding by implementing it. It enabled us to use a single string in a code multiple times instead of writing it again and again. by using multiple examples. The strstr() function enabled us to reduce time complexity in searching for a specific string in huge paragraphs. and with one line statement, we can perform it multiple times in a single code. We don’t have to define them again and again.