Syntax of String find() in C++
size_t find (const string& str, size_t position = 0);
The above syntax is used for C++-style strings. The C++-style strings are objects that are part of the C++ standard library. The C++-style strings use the string class for object creation. Therefore, the <string> header must be included. The standard library string class is accessed through the std namespace.
size_t find (const char* str, size_t position = 0, size_t count);
The above syntax is used for C-style strings (Later, we will see where there is a need to use C-style strings). The C-style strings are one-dimensional arrays of characters. The arrays used here are terminated by a null character ‘\0’. The C-style strings were initially used in the C programming language and are supported in C++ also.
Also, the const is used to pass the string. The string is passed by const reference as we are not changing the string. This saves more time as we directly work on the sting passed with reference.
Parameters
The following are the parameters that are passed in the C++ string find() function:
- Str - The char or string to be searched in the original string.
- Position - It is optional and is used to define the starting position of the search in the original string.
- Count - It is optional and specifies the number of characters of str that we want to match in the original string.
Return Value
The following can be the return value of the C++ string find() function, depending on the inputs.
- The function returns the index to the first occurrence of the substring in the original string.
- If the substring does not exist in the original string, it returns string::npos. The string::npos is a static member associated with the highest value that can be given to the size_t data type.
The return data type is size_t. The size_t is a data type that is returned by the sizeof operator in C++. The sizeof operator is used to find the size of data structures in C++. The size_t operator expresses the size in bytes for any object in C++. It is a variation of the unsigned integer type in C++.
Also check out - Substr C++
Working of C++ String Find with Strings
While working with strings in the C++ string find() function, we will consider the following two cases. We will discuss each of the two cases in detail.
- Searching the substring without defining the count. Here, we will search the whole substring in the given original string.
- Searching the substring defining the count. Here, we will define a count for the substring, and only that many elements of the substring will be searched.
Using C++ String Find() Without Defining Count
The following is a code to understand the workings of the C++ string find function. Here, we will use C++-style strings to find their occurrences in a given string.
Code in C++
C++
#include <iostream>
#include <string>
using namespace std;
// Driver code
int main() {
string original_str = "coding ninjas is the best coding mentorship platform";
string target_str = "coding";
// Find the first occurrence of "coding"
size_t found = original_str.find(target_str);
if (found != string::npos)
cout << "Initial occurrence is at the index " <<
found << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output
Initial occurrence is at the index 0
Here, we can see that the “target_str” first occurrence index is 0 in the “original_str”.
Time Complexity
Here, we can see that the original string is traversed. The string is traversed until we find a matching substring for the target given. In the worst case, we may need to traverse the original string. Thus, the time complexity is O(n), where n is the length of the original string.
Space Complexity
Here, we are not using any extra space for memory allocation. Thus, the space complexity is O(1).
The following code shows the working of the C++ string find function. Here, we will use the C-style strings to find their occurrences in a given string.
Using C++ String Find() With Count
The count is used as an argument when searching for a partial substring in the original string. The count as a parameter cant be used for std::string. Therefore we can only use the C-style strings for using this parameter.
The following code shows the C++ string find() function using count as one parameter.
Code In C++
C++
#include <iostream>
#include <string>
using namespace std;
// Driver code
int main() {
string original_str = "coding ninjas is the best coding mentorship platform";
char target_str[] = "coding_ninjas";
// Find the first occurrence of "coding_ninjas" with only the first six characters
size_t found = original_str.find(target_str, 0, 6);
if (found != string::npos)
cout << "Initial occurrence is at the index " <<
found << endl;
// Find the Next occurrence of "coding_ninjas" with only the first six characters
found = original_str.find(target_str, found + 1, 6);
if (found != string::npos)
cout << "Next occurrence is at the index " <<
found << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output
Initial occurrence is at the index 0
Next occurrence is at the index 26
In the given substring “coding_ninjas,” we defined the count as 6. Therefore the search operation is done only for the initial six characters of the target substring i.e. “coding_ninjas”. To clarify the operation, we have found the next occurrence of the substring.
Time Complexity
Here, we can see that the original string is traversed. The string is traversed until we find a matching substring for the target given. In the worst case, we may need to traverse the original string. Thus, the time complexity is O(n), where n is the length of the original string.
Space Complexity
Here, we are not using any extra space for memory allocation. Thus, the space complexity is O(1)
Working of C++ String Find with Characters
We can work with characters in C++ string find() functions. The character passed can be used to find the occurrence of that character in the original string. The following code can be used to understand the implementation of characters in the C++ string find() function.
Syntax
size_t find (const char c, size_t pos = 0);
Here, unlike strings, we use the data type char in the parameter.
Code in C++
C++
#include <iostream>
#include <string>
using namespace std;
// Driver code
int main() {
string input_string = "coding ninjas code studio library";
char ch = 'n';
// Find the initial occurrence of the character 'n'
size_t check = input_string.find(ch);
if (check != string::npos)
cout << "Inital occurrence of n is at the index " <<
check << endl;
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Output
Inital occurrence of n is at the index 4
Here, in the original string, we search for the target character. As soon as the target character is found, the function returns the position of its occurrence.
Time Complexity
Here, we can see that the original string is traversed. The string is traversed until we find a matching character for the target character given. In the worst case, we may need to traverse the original string. Thus, the time complexity is O(n), where n is the length of the original string.
Space Complexity
Here, we are not using any extra space for memory allocation. Thus, the space complexity is O(1).
Frequently Asked Questions
What does string find() do?
The string find() function in C++ searches for a substring within a string. It returns the position of the first occurrence of the substring in the string, or string::npos if the substring is not found.
How to find a word in a string in C++?
To find a word in a string in C++, you can use the string find() function to search for the word as a substring within the string. If the return value is not string::npos, then the word is present in the string.
How to find specific characters in string in C++?
To find specific characters in a string in C++, you can iterate through the string using a loop and check each character individually. Alternatively, you can use the string find() function to search for a specific substring containing the characters.
How do you check what a string contains in C++?
To check what a string contains in C++, you can use various string manipulation functions such as string find(), string substr(), or iterate through the string and compare characters. These methods help determine if the string contains specific substrings, characters, or patterns.
Conclusion
This article taught us about the C++ string find() function. We learned about the different syntaxes associated with the find() function. We saw the various parameters related to the C++ string find() function and the return value it gives. Lastly, we saw some examples related to the function and discussed the time and space complexity.
To learn more about C++ and String Library functions, Check out some amazing Guided Paths on topics such as Data Structure and Algorithms, Basics of C, Basics of Java, etc., along with some Contests and Interview Experiences.