There are different ways to check if string contains substring in C++. Library functions are available in C++ like find()
and strstr()
to find the substring in a string. In this article, we will discuss multiple methods to check if a string contains a substring in C++.
Table of Contents
Using find() function to check if string contains substring in C++.
We can use string::find()
that can return the first occurrence of the substring in the string. It returns the index from the starting position and the default value for this function is 0. It returns -1 if the substring is not present in the string.
Syntax:
1 2 3 |
__size_t find(const string& str, size_t pos=0) const;__ |
str
: substring to be searched
s
: string in which substring will be searched
pos
: If pos
is mentioned, then it will only include characters that are present on that position or after that position. It will ignore all the characters which are present before the mentioned position.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <string> using namespace std; int main(){ string str1 = "Java2Blog"; string str2= "Blog"; //substring to be checked int position = 0; int index_str; while(( index_str = str1.find(str2, position)) != string::npos){ cout<< "Substring " << "'" << str2 << "'" << " Found at index: " << index_str << endl; position = index_str + 1; } return 0; } |
Output
Using strstr() method to check if string contains substring in C++
strstr()
function can also be used to locate the substring in a string in C++. It is a C-language function. It will return a pointer to the first position of the substring in the mentioned string. It will return a null pointer if the substring is not present in the string. In order to use this function in C++, we will convert the string and substring into a c-string using the c_str()
function.
Note: The matching process will not include terminating null characters but it will stop there.
1 2 3 |
__ const char* strstr (const char * string1, const char * string2);__ |
string1
: substring to be searched
string2
: string in which substring will be searched
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <bits/stdc++.h> using namespace std; int main(){ string str1 = "Java2Blog"; string str2 = "Blog"; //Using strstr() and converting string to c-string. if(strstr(str1.c_str(),str2.c_str())) { cout << "Substring "<<"'" << str2 << "'" <<" is present in " <<str1 << endl; } else{ cout<<"Substring is not present." << endl; } //Checking if python is present in the main string string str3 = "Python"; //Using strstr() and converting string to c-string. if(strstr(str1.c_str(),str3.c_str())) { cout << "Substring "<<"'" << str3 << "'" <<" is present in " <<str1 << endl; } else{ cout<<"Substring is not present." << endl; } return 0; } |
Output
Substring is not present.
Using boost library to check if string contains substring in C++
We can use boost::algorithm::contains()
in the string.hpp header file to check if a string contains substring in C++. This function returns 1 if the string contains a specified substring and 0 if not present in the string.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <string> #include <boost/algorithm/string.hpp> using namespace std; int main() { string str1="Java2Blog"; string str2="Blog"; bool b = boost::algorithm::contains(str1, str2); if(b==1) { cout << "Substring "<<"'" << str2 << "'" <<" is present in " <<str1 << endl; } else { cout<<"Substring is not present."; } return 0; } |
Output
Using custom functions to check if string contains substring in C++
We can also make functions that can check if a substring is present in the specified string. Library functions are not needed in this method. In the first approach, we run a nested loop. The inner loop will give substrings of the specified substring length. We will check for every index if the substring given by the inner loop is the specified substring or not. The time complexity for this method will be O(len1*len2). Here len1 and len2 are the lengths of the string and substring.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
#include <iostream> #include <string> using namespace std; //Return index if substring is present in the string int check_substring(string str1, string str2) { int i,j; int len1 = str1.length(); int len2 = str2.length(); for (i = 0; i <= len2 - len1; i++) { for (j = 0; j < len1; j++) if (str2[i + j] != str1[j]) break; if (j == len1) return i; } return -1; } int main() { string str1 = "2"; string str2 = "Java2Blog"; string str3 = "Java1"; int position = check_substring(str1, str2); if (position == -1) {cout << "Substring is not present";} else {cout << "Substring is present at index " << position << endl;} //checking for substring "Java1" int position2 = check_substring(str3, str2); if (position2 == -1) {cout << "Substring is not present";} else {cout << "Substring is present at index " << position2 << endl;} return 0; } |
Output
Substring is not present
We can also follow a second approach where we need to run only one for loop for traversing the main string. We will take a pointer for substring starting from the first index. We will compare every character in the string and check with the pointer at the substring. The pointer will increase by one if the character matches or it will be reset to zero. We also need to check if the pointer at substring has a value equal to the length of the substring, then return true. This approach is more efficient than the first approach and its time complexity is O(n).
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <iostream> #include <string> using namespace std; //returns index if substring is present else -1. int check_substring(string str1, string str2) { int i; int c = 0; // counter for substring for( i=0;i<str1.length();i++) { if(c==str2.length()) { break; } if(str2[c]==str1[i]) { c++; } else { // if the character next to ith character is duplicate if(c > 0) { i -= c; } c = 0; } } //checking if the substring is present or not if(c < str2.length()) { return -1; } else{ return i-c; } } int main() { string str1 ="Java2Blog"; string str2= "Java2"; int index= check_substring(str1, str2); if(index==-1){ cout<<"Substring is not present in the string."; } else{ cout<<"Substring " <<"'" << str2 <<"'"<< " is present in the string at index: " << index << endl; } return 0; } |
Output
Conclusion
In this article, we discussed several ways to check if a string contains a substring or not. Any approach can be followed as per the requirements. C++ 23 has a library function contains()
which can also be used to solve this problem.
Happy Learning!!