Strstr in C

Strstr() in the C language is a built-in function. The functionality of strstr can be understandable through the manual present in the Ubuntu terminal if you are working on Ubuntu. Then the terminal will show you the guide of strstr and how it works.

$ man strstr

Example 1

Consider the first example of strstr; we have used the code in a file. And we will get the output through this file in the terminal. As it is known that the input of strstr are two strings, in which one string’s occurrence is identified in the other string. Firstly the library header “string.h” will be used that handles many functions of string. If this library is not introduced, it is not possible to execute a program of string functions. The string function used in this source code is

P = strstr (s1, s2)

In this, p is a pointer. S1 and S2 are two strings. We need to find the occurrence of s2 in the string s1. To print out the results, we have used an if-else statement to apply the condition that checks the first occurrence of the string. If the particular substring is present in the main string, it will be displayed with a confirmation message. If it is not present, then a message will be displayed.

In the above screenshot, you can see an input string from which you need to identify the small string. That small string is also mentioned if the statement will take p as an argument in the parameter, as the value of the strstr function is stored in it.

If you are working on Linux operating system and want to get the output. Then this can be done by using a couple of commands on the Ubuntu terminal. The first command is for the compilation

$ GCC –o file9 file9.c

For compilation, we need a compiler, the GCC used for Linux to compile a C program on it. “-o” is used to store the result in an output file from the source file.  Now the next step is the execution.

$ . /file8

This is completed by using a dot method. In which we use a dot and a slash with the name of the file.

The output shows that the substring was present and also shows its location in the file.

Example 2

This is another simple example of the strstr() function without the use of if-statement. In this C program, we will match a particular word in the string and then fetch it as the word is matched according to its occurrence. Then in the output, the word and the characters present along with the substring are also displayed.

Output = strstr(a, searchstring);

As the occurrence is obtained by using the pointer variable in the code. Because this pointer is used to get the location of the substring and to see the substring, we use only the variable name without asterisk in the output command. If we want to display the location, then we will use the pointer( variable with an asterisk), i.e., *output.

You can see this in the output. The word ”function” was to be searched as a substring. The characters, along with the substring, are also displayed.

Example 3

In this code, we will first find the occurrence of the sub-string, and then this sub-string will be replaced with another string. Again two strings will be reserved as input. One is the big string, and the other is a word that will be replaced after its occurrence will be determined. The strstr function matches the small sub-string with the original one. And when the match is founded for the very first time, it returns the value. But in this example, this value is further replaced. Let’s see how it will work.

P = strstr(s1, s2);

Where p will store the value of occurrence in it, S1 and s2 are the input strings.

Now we have the point of occurrence of that input string. Now we will replace this string with the other word. This is done in the body of the if statement. This shows that if the condition is true, the word is founded, then replaced by another word. This replacement is done through another string function.

Strcpy (p, “strstr”)

We want to replace the word with “strstr”. Where p is the location of the first occurrence of that substring which the function has replaced. Strcpy() replaces these two words in the string. The output will be obtained through the same compile-execution method.

From the output, you can see that the string is now replaced with the other word we have described in the strcpy function.

Example 4

This example shows the same concept. Here we have taken free space as a character in addition to the word as a substring. It is a simple illustration in which we don’t even have used if-statement. Only the match and display concept is applied. Two strings are taken as input. In addition, the word is displayed with the message. The strstr works in the same manner.

c= strstr (a, b);

Here c is the variable where the occurrence point will be stored.

Now, we will obtain the output.

From the output, you can notice that space is also counted with the substring we have introduced.

Example 5

This example is quite different from the previous ones. Here we have used a separate function to perform the action of strstr() instead of the main program. In comparison, the values will be passed as arguments in the parameters of the function call. In this example, we have mentioned the substring, and the program, after execution, will show the position of the first occurrence instead of the value in the output. The function will receive the values in the variables, and then we will apply the strstr() on these variables. If-else statement is used to check the availability and to make the condition true and if it is false, then move to the else part.

Char* pos = strstr(str, substr);

Whereas str is a string, substr is a substring. Char*pos is the position of the first occurrence of a substring in the string. The sign ‘%s’ present in the statement that is displayed implies replacing a substring and the whole string. As substring is present before the string in the parameter.

Now moving towards the main program. Firstly a string is introduced that will be passed through a function call

Find_str( str , “best”);

Here we have also added the substring with the string. Each time a new substring is added. The second time we have added a free space character. The third time, a substring that is not a part of the string is added. And in the last, an alphabet is used.

Use the commands and then see the result appended below.

Here is the result of the C program function calls. The first two and 4th statements satisfy the condition, so the answer is displayed. The third one is not relevant, so the else part will handle this.

Conclusion

In this article, the usage of strstr is discussed, along with examples. These examples show variety in using that concept in many ways. The string functions are easy to use in the presence of their header in the library.



from Linux Hint https://ift.tt/3BBO8eB

Post a Comment

0 Comments