Home > Backend Development > C++ > body text

strstr() function in C/C++

WBOY
Release: 2023-09-16 22:37:02
forward
1674 people have browsed it

strstr() function in C/C++

The strstr() function is a function predefined in the "string.h" header file and is used to perform string processing. This function is used to find the first occurrence of a substring (e.g. str2) within a main string (e.g. str1).

Grammar

The syntax of strstr() is as follows:

char *strstr( char *str1, char *str2);
Copy after login

The parameters of strstr() are

str2 is what we want in The substring searched for in the main string str1

The return value of strstr() is

If the substring we are searching for is found in the main string , the function will return the address pointer of the substring; otherwise, when the substring is not in the main string, it will return a null pointer.

Note - The matching process does not include the null character (‘\0’), but stops when it encounters the null character.

Example

Input: str1[] = {“Hello World”}
str2[] = {“or”}
Output: orld
Input: str1[] = {“tutorials point”}
str2[] = {“ls”}
Output: ls point
Copy after login

Example

Live Demonstration

#include <string.h>
#include <stdio.h>
int main() {
   char str1[] = "Tutorials";
   char str2[] = "tor";
   char* ptr;
   // Will find first occurrence of str2 in str1
   ptr = strstr(str1, str2);
   if (ptr) {
      printf("String is found\n");
      printf("The occurrence of string &#39;%s&#39; in &#39;%s&#39; is &#39;%s&#39;", str2, str1, ptr);
   }
   else
      printf("String not found\n");
   return 0;
}
Copy after login

Output

If we run the above code, it will generate the following output-

String is found
The occurrence of string &#39;tor&#39; in &#39;Tutorials&#39; is &#39;torials
Copy after login

Now, let us try another application of strstr()

We can also use this function to replace a certain part of the string, for example, if we Want to replace the string str1 after finding the first substring str2.

Example

Input: str1[] = {&ldquo;Hello India&rdquo;}
str2[] = {&ldquo;India&rdquo;}
str3[] = {&ldquo;World&rdquo;}
Output: Hello World
Copy after login

Explanation − Whenever the str2 is found in str1 it will be substituted with the str3

Example

Real time Demo

#include <string.h>
#include <stdio.h>
int main() {
   // Take any two strings
   char str1[] = "Tutorialshub";
   char str2[] = "hub";
   char str3[] = "point";
   char* ptr;
   // Find first occurrence of st2 in str1
   ptr = strstr(str1, str2);
   // Prints the result
   if (ptr) {
      strcpy(ptr, str3);
      printf("%s\n", str1);
   } else
      printf("String not found\n");
      return 0;
}
Copy after login

Output

If we run the above code it will generate the following output-

Tutorialspoint
Copy after login

The above is the detailed content of strstr() function in C/C++. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template