In C++, string find() is a built-in library function used to find the first occurrence of a substring in the given string. Let’s take a look at a simple example that shows the how to use this function:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "Welcome to GfG!";
string sub = "to";
cout << s.find(sub);
return 0;
}
Explanation: The function returned the starting index of the first occurrence of substring sub in the string s.
Syntax
string find() is a member function of std::string class defined inside <string> header file and have 3 implementations.
C++
s.find(sub, pos); // For substring
s.find(sub, pos, n); // For n character of sub
s.find(c, pos); // For character
The 2nd implementation only works with C style strings.
Parameters
- s: String which is to be searched.
- sub: Substring to search. Can be C++ or C style string.
- pos: Position from where the string search is to begin. By default, it is 0.
- n: Number of characters to match.
Return Value
- Returns the integer representing the index of the first occurrence of the sub-string.
- If the sub-string is not found, it returns string::npos.
To learn how to use the find
function effectively, the C++ Course provides practical examples and detailed explanations.
Examples of string find()
The following examples demonstrate the use of string find() function for different purposes.
Check if Substring Exists
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "Welcome to GfG!";
string sub = "hello";
// Checking if sub is present in s
int res = s.find(sub);
if (res != string::npos)
cout << res;
else
cout << sub << " NOT found.";
return 0;
}
Explanation: In the above program we checked the string s to find the substring sub using the string find() function. As the string s does not contains any instance of string sub the string find() function returns string::npos.
Find Multiple Occurrences of a Substring
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "welcome to geeksforgeeks";
char sub[] = "geeks";
// Loop that runs till string::find()
// returns string::npos
int res = -1;
while ((res = s.find(sub, res + 1)) !=
string::npos)
cout << res << " ";
return 0;
}
Explanation: In the above program a loop is used that runs till string find() returns string::npos and in each iteration, the part of the string where the previous occurrence was found is not considered in the search. In this way the program finds all the occurrences of the string sub withing the string s.
Find All the Occurrences of a Character
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "welcome to GfG";
char c = 'e';
// Loop that runs till string::find()
// returns string::npos
int res = 0;
while ((res = s.find(c, res + 1)) !=
string::npos)
cout << res << " ";
return 0;
}
Explanation: The above program uses a loop that runs till string find() returns string::npos and within the loop a string find function is used which finds the occurrences of the character 'c' in the given string , after each iteration,part of the string where the previous occurrence of the character was found is not considered in the search. In this way the program finds all the occurrences of the character c withing the string s.
Find Occurrence of a Partial Substring
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "welcome to GfG!";
char sub[] = "come to my house";
// Only searching for "come"
int res = s.find(sub, 0, 4);
if (res != string::npos)
cout << res;
return 0;
}
Explanation: The third parameter in the string find() function is used to specify the first n characters of the substring to be matched. we have matched the first 4 characters of the substring in the above program.
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read