Check if string contains substring in C++

String contains C++

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++.

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:

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:

Output

Substring ‘Blog’ Found at index: 5

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.

string1: substring to be searched
string2: string in which substring will be searched

Code:

Output

Substring ‘Blog’ is present in Java2Blog
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:

Output

Substring ‘Blog’ is present in Java2Blog

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:

Output

Substring is present at index 4
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:

Output

Substring ‘Java2’ is present in the string at index: 0

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!!

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *