| by Arround The Web | No comments

C++ Contains Examples

The string data type in C++ allows us to apply several functions to perform various activities with the strings. Whether or not the substring is contained in the original string can be determined by checking it. The C++ language facilitates us with various functions that aid in finding whether the string contains the substring or not. The contains() function is one of them that helps in doing this task. This feature is only available in C++ 23. We will learn in detail how this contains() function allows us to find whether or not the substring is present in the original string.

Example 1:

In this situation, we must work with strings and input or output data, so the “iostream” and “string” header files are provided. Thus, these header files need to be included here. Afterward, we no longer need to include this “std” with each function independently in our code because we already included the “namespace std” with the aid of the “using” keyword. Here, the “main()” function is then called. Then, the “string” variable named “originalString” is initialized with some string. Then, we initialize another “string” variable named “word” with C++.

Now, underneath this, we utilize the “cout” and print that original string. After this, we declare the “int Result” and place the “contains()” function to check whether the “originalString” contains the “word” or not. We place “if” below it,. We pass the “Result” to “if”. If the original string contains the substring, it renders the statement that we added after “if”. In the case when the string doesn’t contain the substring, the statement that is presented after “else” is rendered.

Code 1:

#include <iostream>

#include <string>

using namespace std;

int main()

{

  string originalString = "I like C++ Programming";

  string word = "C++";

  cout << "The string is = " << originalString << endl << endl;

  int Result = originalString.contains(word);

  if (Result)

  {

    cout << "The word found in the string which is = " << word << endl;

  }

  else

  {

    cout << "The word not found in the string" << endl;

  }

  return 0;

}

Output:

This outcome shows that the substring that we find in the original string with the aid of the contains() function is found inside the original string and is displayed here.

Example 2:

The “iostream” and “string” are the header files that we imported in this code. The “std” namespace is also included. Then, main() is called here. Next, we initialize the “string” variable called “str_1” by adding some string data. Hereafter, we initialize the “string” variable called “str_2” with “Rain”.

Below this, we print the original string using the “cout” function. To determine whether or not “str_2” is present in “str_1”, we declare the “int Outcome” and insert the contains() method here. Below, we place the “if” and pass the “Outcome” to “if”. We add the “cout” statement after “if” is rendered if the original string has the substring. Otherwise, the “cout” statement that comes after “else” is rendered.

Code 2:

#include <iostream>

#include <string>

using namespace std;

int main()

{

  string str_1 = "The weather is cool outside";

  string str_2 = "Rain";

  cout << "The string is = " << str_1 << endl << endl;

  int Outcome = str_1.contains(str_2);

  if (Outcome)

  {

    cout << "The word found in the string which is = " << str_2 << endl;

  }

  else

  {

    cout << "The word not found in the string" << endl;

  }

  return 0;

}

Output:

This renders that the substring that we find in the original string with the help of using the contains() function is not found inside the original string and is rendered here as a result.

Example 3:

We import the “iostream” and “string” header files in this code. Then, below this, we use the “namespace std”. Then, main() is invoked here. Now, we initialize the two variables of the “string” data type with the names “myString_1” and “myString_2”, respectively, and assign the original string from which we want to find the substring to “myString_1” variable and the substring is assigned to the “myString_2” variable.

After this, we display the original string by putting “myString_1” in the “cout” statement and then placing “if” in which we utilize the “contains()” method that checks whether the given string contains the substring. If the substring is present in the original string, with the help of “cout”, we render the result.

We place two “cout” statements in which one is placed after “if” and the other is added after the “else” part. If the original string contains the substring, the “cout” after “if” will be rendered. If the substring is not found or the original string doesn’t have the substring, the “cout” after “else” will be rendered.

Code 3:

#include <iostream>

#include <string>

using namespace std;

int main()

{

  string myString_1 = "Hi! Hello World";

  string myString_2 = "Hello";

  cout << "The complete string is " << myString_1 << endl;

  if (myString_1.contains(myString_2)) {

  cout << "The String found = " << myString_2 << endl;

  }

  else {

    cout << "The String not found here " << endl;

  }

 

  return 0;

}

Output:

This result demonstrates that the substring that we search within the original string using the contains() method is located within the original string and it is shown here.

Example 4:

The “iostream” and “string” header files are imported into this code. The main() function is then called here after we utilize the “namespace std” underneath. The initializations of the two variables of the “string” data type are named “s_1” and “s_2”, respectively. The original string from which we wish to discover the substring is now assigned to the “s_1” variable, and the substring is given to the “s_2” variable. The original string is then shown by inserting “s_1” into the “cout” statement.

Next, we add the “if” clause where we use the contains() method to determine whether the supplied string contains the substring. We render the output using “cout” if the substring is present in the original string. Two “cout” statements are added, one after “if” and another after the “else” portion of the code. The “cout” following “if” is rendered if the substring is present in the original string. Otherwise, the “cout” following “else” is rendered if the substring cannot be located in the original string.

Code 4:

#include <iostream>

#include <string>

using namespace std;

int main()

{

  string s_1 = "Hi! I'm Jack here'";

  string s_2 = "Peter";

  cout << "The complete string is = " << s_1 << endl;

  if (s_1.contains(s_2)) {

    cout << "The String found = " << s_2 << endl;

  }

  else {

    cout << "The String not found = " << s_2 << endl;

  }

  return 0;

}

Output:

As seen from this result, the substring that we searched for within the original text using the contains() method is not found in the code.

Example 5:

Here, we will utilize the “boost” library and find whether the string contains the substring or not. In this “boost” method, we also utilize the contains() function. So, we also include the “boost/algorithm/string.hpp” header file along with the “iostream” and “string” header files in this code. We then place the “std” and invoke the main() method here.

Then, we declare the variables which are “StringData1” and “StringData2” of the “string” data type and initialize the string data here. The “c_result” variable of the “bool” data type is initialized with the “boost::algorithm::contains()” and we pass the “StringData1” and “StringData2” into this function. This also finds the substring in the original string and store the Boolean result in the “c_result” variable.

Now, we place the “c_result==1” in the “if” below. If the Boolean value of the “c_result” is “1”, the statement after “if” is displayed. Otherwise, it moves towards the “else” part and skips the statement which is present after “if” and renders the “else” part’s statement.

Code 5:

#include <iostream>

#include <string>

#include <boost/algorithm/string.hpp>

using namespace std;

int main() {

  string StringData1 ="My first Program";

  string StringData2 ="first";

  bool c_result = boost::algorithm::contains(StringData1, StringData2);

  if(c_result==1)

  {

    cout << "The string "<<"'" << StringData1 << "'" <<" contains " <<StringData2 << endl;

  }

  else

  {

    cout<<"The given word is not present in the string.";

  }

  return 0;

}

Output:

The substring is now found in the original string which we can see in this outcome.

Conclusion

We explored the contains() method provided by the C++ language in detail. We also mentioned that the contains() function is only available in “C+++ 23”. We explored whether the contains() function aids in finding the substring in the original string or if it aids in checking whether the string contains the substring or not and rendered the result accordingly.

Share Button

Source: linuxhint.com

Leave a Reply