Jump to content

C++ String Contains a Substring


Linux Hint

Recommended Posts

In C++, we have the string data type and can apply the different functions to do the various tasks with the strings. We can check whether the substring is present inside the original string. We can also say that the “string contains the substring”. In this guide, we will learn the techniques that aid us in finding the “string contains the substring”. The “find()” and “strstr()” functions help us in doing this task in C++ programming.

Example 1:

The “iostream” and “string” are the header files that are included here as we have to work with the strings and also need to input or output the data. So, we must include these header files here. After this, we include the “namespace std” with the help of the “using” keyword. So, we don’t need to put this “std” with all functions separately in our code. Then, the “main()” function is invoked here.

Now, we declare the “str_1” string and assign some string data to this variable. Then, we also initialize another variable named “str_2” of the “string” data type and assign “like” to this “str_2” variable. Below this, we utilize the “bool” keyword to give true or false results. We initialize the “stringHasStr” with this “bool” data type and utilize the “find()” function. This searches the “string contains the substring”. The “str_1” is the complete string and “str_2” is the substring.

Here, we also add the “npos” keyword which is utilized to show that there are no matches in this string. It checks whether the string contains a substring and stores the result in this “stringHasStr” bool variable.

Then, we move ahead towards the “if” condition and pass this “stringHasStr” variable to this “if” condition. If the result that is stored in this bool variable is “true”, the statement after this “if” condition is employed where we utilize the “cout” and display the string that is found here. But if the “false” result is stored in this bool variable, the other part is employed and display that the string is not found here.

Code 1:

#include <iostream>
#include <string>

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) {
        cout << "We find the string here which is " << str_2 << endl;
    }
    else {
        cout << "The String not found" << endl;
    }
    return 0;
}

 

Output:

This given output renders that the string contains the substring and displays it here. We check this by utilizing the “find()” function.

C-String-Contains-a-Substring-1.png

Example 2:

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

Next, we initialize a second variable of the “string” data type called “sub_str” and give it the “very” value. Then, we place the “const char*”. So, changing the pointer’s value to point to the other location in the memory is impossible. We declare the “FindingStr” variable as this “const char*” pointer here. We initialize it with the “strstr()” method and pass both strings along with the “c_str()” function that transforms a string into a character array that ends with a null value. This “strstr()” method aids in checking whether the “new_str” string contains the “sub_str” substring or not. Then, we have “if” in which we add the “FindingStr”. If it finds the substring in the original, the statement after “if” is executed where we utilize the “cout”. If the substring is not found, it directly moves toward the “else” part and prints the result which is placed after the “else” part.

Code 2:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;
int main()
{
    string new_str = "It is raining outside and the weather is very pleasant.";
    string sub_str = "very";
    const char* FindingStr = strstr(new_str.c_str(), sub_str.c_str());
    if (FindingStr) {
        cout << "We find the String and the string is: " << sub_str << endl;
    }
    else {
        cout << "String not found" << endl;
    }
    return 0;
}

 

Output:

We might notice that the given string contains the substring as it displays the statement that we added after “if” and prints the substring here. We check this by utilizing the “strstr()” function.

C-String-Contains-a-Substring-2.png

Example 3:

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

Below this, we utilize the “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 place the “npos” keyword which checks that the result of the “find” function is not equal to “npos”. After this, we utilize the “cout” which increments the index value by one and stores it in the “posOfStr” variable.

Code 3:

#include <iostream>
#include <string>

using namespace std;
int main(){
    string myNewStr = "we are finding the string here";
    string mySubStr = "string";
    int posOfStr = 0;
    int indexOfStr;
    while(( indexOfStr = myNewStr.find(mySubStr, posOfStr)) != string::npos){
        cout<< " The substring is " << "'" << mySubStr << "'" << " Found at index number : " << indexOfStr << endl;
        posOfStr = indexOfStr + 1;
    }
        return 0;
}

 

Output:

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

C-String-Contains-a-Substring-3.png

Example 4:

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

After this, we declare the “sub_str2” variable and assign a string data here. The “if” condition is then inserted, and the “strstr()” function is used. This method searches whether or not 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 only run if the “if” condition is not met or if the substring cannot be located in the string.

Code 4:

#include <bits/stdc++.h>
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()))
    {
        cout << "The Substring here is "<<"'" << sub_str << "'" <<" is present in " << org_str << endl;
    }
    else{
        cout<<"Substring is not present in the string." << endl;
    }
string sub_str2 = "Java";

    if(strstr(org_str.c_str(),sub_str2.c_str()))
    {
         cout << "The Substring here is "<<"'" << sub_str2 << "'" <<" is present in " << org_str << endl;
    }
    else{
        cout<<"Substring is not present in this string." << endl;
    }
return 0;
}

 

Output:

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

C-String-Contains-a-Substring-4.png

Conclusion

The concept of “string contains the substring” is thoroughly examined in this guide. We explored two methods that aid in searching whether the “string contains the substring”. We explained the “find()” as well as the “strstr()” functions that C++ provides here in this guide for doing this task. We demonstrates with unique examples in which we learned how to utilize these functions to check whether the “string contains the substring” in it.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...