Loading
svg
Open

C++ string has a substring

January 21, 20249 min read

In C++, we have String data type and can apply various functions to perform various operations with strings. We can check whether the substring exists inside the original string or not. We can also say that “the string contains substrings”. In this guide, we will learn the techniques that help us find “string contains substring”. “find()” and “strstr()” functions in C++ programming help us in performing this task.

Example 1:

“IOStream” and “String” are header files that are included here because we have to work with strings and also need to input or output data. So, we need to include these header files here. After this we include “namespace std” with the help of “using” keyword. Therefore, we do not need to keep this “std” separately with all the functions in our code. Again, the “main()” function is implemented here.

Now, we declare the string “str_1” and assign some string data to this variable. Then, we also initialize another variable named “str_2” of “String” data type and assign “like” to this “str_2” variable. Below this, we use the “bool” keyword to return true or false results. We initialize “StringHaStr” with this “bool” data type and use the “find()” function. It searches for “string contains substring”. “str_1” is the full string and “str_2” is the substring.

Here, we also add the keyword “npos” which is used to show that there is no match in this string. It checks if the string contains a substring and stores the result in this “stringhasstr” bool variable.

Then, we proceed to the “if” condition and pass this “stringHasStr” variable into this “if” condition. If the result stored in this bool variable is “true”, then the statement following this “if” condition is employed where we use “cout” and display the string found here. But if the “false” result is stored in this bool variable, the second part is employed and displays that the string is not found here.

Code 1:

#include
#Involved <स्ट्रिंग>

using namespace std;
int main,,
,
string str_1 = “I like C++ programming language”,
string str_2 = “like”,
bool stringhasstr ​​= str_1.find,str_2, ,= string::npos;
If ,stringHasStr, ,
court , “We get the string here that is” , str_2 , Andal;
,
Other ,
court , “string not found” , Andal;
,
return 0,
,

Output:

This given output presents whether the string contains a substring and displays it here. We check it using the “Find()” function.

Example 2:

We include three header files here which are “iostream”, “string” and “cstring”. Then, invoke “main()” after placing “namespace std”. The “new_str” string is now declared and assigned some string data.

Next, we call another variable of “string” data type “sub_str” and give it the value “much”. Then, we keep “const char*”. Therefore, it is impossible to change the value of a pointer to another location in memory. We declare the “FindingStr” variable here as this “const char*” pointer. We initialize it with the “strstr()” method and pass both strings to the “c_str()” function which converts a string to a character array that ends with a null value. This “strstr()” method helps to check whether “new_str” string contains substring “sub_str” or not. Then, we have “if” to which we add “FindingStr”. If it finds the substring in the original, the statement after the “if” is executed where we use “cout”. If the substring is not found, it moves directly to the “else” part and prints the result which is placed after the “else” part.

Code 2:

#include
#Involved <स्ट्रिंग>
#include

using namespace std;
int main,,
,
string new_str = “It is raining outside and the weather is very pleasant.”,
string sub_str= “Very”,
constant four, findStr = strstr,new_str.c_str,,sub_str.c_str,,,,
If ,findStr, ,
court , “We find the string and the string is:” , sub_str , Andal;
,
Other ,
court , “string not found” , Andal;
,
return 0,
,

Output:

We can see that the given string contains the substring as it displays the statement which we have added after “if” and prints the substring here. We check this using the “strstr()” function.

Example 3:

We initialize two string variables here: “myNewStr” and “mySubStr”. Then, we specify some string data and declare two integer variables: “posOfStr” and “indexOfStr”.

Below this, we use a “while()” loop and assign the “find()” function variable to the “indexOfStr” variable inside this “while()” loop. We pass two variables to this “find()” function which are “mySubStr” and “posOfStr”. Then, we insert the keyword “npos” which checks that the result of the “find” function is not equal to “npos”. Next, we use “cout” which increments the index value by one and stores it in the “posOfStr” variable.

Code 3:

#include
#Involved <स्ट्रिंग>

using namespace std;
int main,,,
string myNewStr = “We are looking for the string here”,
string mySubStr = “string”,
int posOfStr = 0,
int IndexOfStr;
Whereas,, IndexOfStr = myNewStr.find,mySubStr,posOfStr,, ,= string::npos,,
court, ” is substring “ , , , mySubStr , , , “Found at index number:” , IndexOfStr , Andal;
posOfStr = indexOfStr + 1,
,
return 0,
,

Output:

Here, it shows that the given string has substring and the index number of this string is “19” which is also displayed in this result.

Example 4:

In this code, we include the “bits/stdc++.h” header file. Now, we do not need to include other header files as they contain all the required libraries. After invoking “main()”, we initialize “org_str” and “sub_str” variables of “string” data type. Then, we add an “if” condition in which we use the “strstr()” function. This function searches to see if a given string contains the desired substring. Then, we add a “cout” statement to print that the substring has been found here. Then, we also put the “else” part which is executed only if the “if” condition is not satisfied or the substring is not found in the string.

Next, we declare the “sub_str2” variable and assign a string data here. Then the “if” condition is inserted, and the “strstr()” function is used. This method searches whether the supplied string contains the requested substring. The result is then printed here using the “cout” command. We also include an “else” section that is run only if the “if” condition is not met or if the substring cannot be located in the string.

Code 4:

#includes

using namespace std;
int main,,,

string org_str = “C++ Programming Language”,
string sub_str= “Program”,
If,strstr,org_str.c_str,,,sub_str.c_str,,,,

,

court , “here is the substring”,, , sub_str , , ,“exists in” , org_str , Andal;
,

Other,

court,“Substring does not exist in string.” , Andal;
,

string sub_str2 = “Java”,

If,strstr,org_str.c_str,,,sub_str2.c_str,,,,
,
court , “here is the substring”,, , sub_str2 , , ,“exists in” , org_str , Andal;
,
Other,
court,“Substring does not exist in this string.” , Andal;
,
return 0,
,

Output:

The result of the first “strstr()” function shows that the substring is present in the string, but the result of the second “strstr()” function shows that the substring is not present in the string.

conclusion

The concept of “string contains substrings” is thoroughly examined in this guide. We explored two methods that help find if “string contains substring”. We explained about the “find()” as well as “strstr()” functions that C++ provides in this guide to perform this task. We demonstrate with unique examples in which we learned how to use these functions to check whether “string contains substring”.

How do you vote?

0 People voted this article. 0 Upvotes - 0 Downvotes.
svg

What do you think?

Show comments / Leave a comment

Leave a reply

You may like
Loading
svg