C++ String find() function

In this tutorial, We will be learning about the C++ string find() function. First, let us know something about it.

String find() function in C++

String find() function is used to find a given substring in a given string. It returns the index of the first character in the substring present in the Strings.

Below is given the Syntax

Let us consider two strings str1 and str2
So the SYNTAX: str1.find(str2)

 

Let us consider some examples.

Let us take an input of two strings 
str1= "google chrome "
str2 = "ro"
 
output: 'I' found at Index 9

IMPLEMENTATION BY THE  String find() function

#include<iostream>  
using namespace std;

// Driver function  
int main()  
{  

// Taking Input of both Strings 
string str= "google chrome";  
string str1 = "hr";

 // Applying C++ String find() function
 
cout <<" The Position of the string is :"<< str.find(str1);
  

return 0;   
}

OUTPUT

The Position of the string is: 9

DISCUSSION ABOUT CODE

In the above code, We gave input of two strings as “google chrome” and “hr”. Now apply find() function to find and return the index of the substring in the given string. So we get the desired output.

Also read: How to add space after dot(.) or comma(,) in C++ strings

Leave a Reply

Your email address will not be published.