string find in C++

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).
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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);
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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);
filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.