Skip to content
Related Articles

Related Articles

Improve Article
string find in C++
  • Difficulty Level : Easy
  • Last Updated : 07 Feb, 2019

String find is used to find the first occurrence of sub-string in the specified string being called upon. It returns the index of the first occurrence of the substring in the string from given starting position. The default value of starting position is 0.

Function Template:

  • size_t find (const string& str, size_t pos = 0);
  • size_t find (const char* s, size_t pos = 0);

Function parameters:

  • str : The sub-string to be searched.
  • s : The sub-string to be searched, given as C style string.
  • pos : The initial position from where the string search is to begin.

Function Return:

  • The function returns the index of the first occurrence of sub-string, if the sub-string is not found it returns string::npos(string::pos is static member with value as the highest possible for the size_t data structure).




// CPP program to demonstrate working of string
// find to search a string
#include <iostream>
#include <string>
  
using namespace std;
  
int main()
{
    string str = "geeksforgeeks a computer science";
    string str1 = "geeks";
  
    // Find first occurrence of "geeks"
    size_t found = str.find(str1);
    if (found != string::npos)
        cout << "First occurrence is " << found << endl;
  
    // Find next occurrence of "geeks". Note here we pass
    // "geeks" as C style string.
    char arr[] = "geeks";
    found = str.find(arr, found+1);
    if (found != string::npos)
        cout << "Next occurrence is " << found << endl;
  
    return 0;
}
Output:



First occurrence is 0
Next occurrence is 8

We can also use it to find occurrence of a character:
In below syntax, note that c is a character.

  • size_t find (const char c, size_t pos = 0);




// CPP program to demonstrate working of string
// find to search a string
#include <iostream>
#include <string>
  
using namespace std;
  
int main()
{
    string str = "geeksforgeeks a computer science";
    char c = 'g';
  
    // Find first occurrence of 'g'
    size_t found = str.find(c);
    if (found != string::npos)
        cout << "First occurrence is " << found << endl;
  
    // Find next occurrence of 'g'
    found = str.find(c, found+1);
    if (found != string::npos)
        cout << "Next occurrence is " << found << endl;
  
    return 0;
}
Output:
First occurrence is 0
Next occurrence is 8

We can also search for a partial string
In below syntax, note that n is number of characters to match.

  • size_t find (const char *str, size_t pos, size_t n);




// CPP program to demonstrate working of string
// find to search a string
#include <iostream>
#include <string>
  
using namespace std;
  
int main()
{
    string str = "geeksforgeeks a computer science";
  
    // Only search first 5 characters of "geeks.practice"
    size_t found = str.find("geeks.practice", 0, 5);
    if (found != string::npos)
        cout << found << endl;
  
    return 0;
}
Output:
0
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :