strstr() Function in C and C++ Tutorial with Examples


C and C++ programming languages provide the strstr() function in order to find or match a string in another string. For example, we can search pof string inside the poftut.com and find matches and return the matching index number.

strstr() Function Syntax

strstr() function has the following syntax where two strings are provided as parameter. strstr() function is case sensitive which means uppercase and lower case matters. For example  pof is will not math with Pofor POF etc.

const char *strstr(const char *STR1, const char *STR2)
  • `const char *strstr` is the function that will return a pointer or handle as the char data type for the match. If there is no match it will return a null pointer.
  • `const char *STR1` is the string where we will search the STR2. It is constant char pointer simply a string in C and C++.
  • `const char *STR2` is the term or string which will be searched in STR2.

strstr() Function Matching Example

We will create a simple example where we will search poftut.com string or char array in the I love the poftut.com string or char array. Before starting in C and C++ string and char array are the same thing just they have a different name but under the hood, they are the same.

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
   //String to search in
   char str1[] ="I love poftut.com web site";

   //Result pointer
   char *result;

   //Use strstr() function to search "poftut.com" 
   //and store result into result variable
   result = strstr (str1,"poftut.com");

   //Print result to the standart output
   //This will print characters from first occurence 
   //to the end
   //output is: poftut.com web site
   puts(result);

   return 0;
}

strstr() Function NonMatching Example

In this case, we will make an example where search terms or string do not match or found in the given string. We will search kaleinfo.com inside the string I love poftut.com web site.

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
   //String to search in
   char str1[] ="I love poftut.com web site";

   //Result pointer
   char *result;

   //Use strstr() function to search "kaleinfo.com" 
   //and store result into result variable
   result = strstr (str1,"kaleinfo.com");

   //Create an error because result is null pointer
   puts(result);

   return 0;
}

This example will create an exception because the result is null and when we try to print the result it will create an error or exception.

LEARN MORE  Python Variables and Types

Use strstr() Function For String Replacement

Another useful case for strstr() function is using it for string replacement. We can find the specified string and replace it with the given new string. We will also use the strncpy() function to replace string. We will use I love poftut.com web site and replace the poftut.com with the kaleinfo.com.

/* strstr example */
#include <stdio.h>
#include <string.h>

int main ()
{
   //String to search in
   char str1[] ="I love poftut.com web site";

   //Result pointer
   char *result;

   //Use strstr() function to search "poftut.com" 
   //and store result into result variable
   result = strstr (str1,"poftut.com");

   //Replace kaleinfo.com with poftut.com
   strncpy(result,"kaleinfo.com",12);

   //Print result to the standart output
   //This will print characters from first occurence 
   //to the end
   // Output will be: kaleinfo.comeb site
   puts(result);

   return 0;
}

strstr() Function in PHP

With the same name and syntax, PHP programming language also provides the strstr() function. This function can be used in PHP version 5.3 and above. In the following example, we will find the user name from the email address and print to the string.

<?php
$email_address  = '[email protected]';
$domain_name = strstr($email_address, '@');
echo $domain_name; // prints @poftut.com

$user_name = strstr($email, '@', true); 
echo $user_name; // prints name ismail
?>

Leave a Comment