Print all strings in the given array that occur as substrings in the given string using C++


In the world of programming, there are many of the scenarios where we really want to look for specific patterns within a larger text. One common task is to find and print every one of the strings from a given array that occur as substrings in a given string. This apparently basic problem can be tackled utilizing various methodologies, and in this article, we will explore two of them. We will give a definite clarification of the syntax and algorithm utilized for each methodology, alongside two full executable code examples.

Syntax

Before we deliver into the approaches, let's first understand the syntax which we will be using to solve this problem −

void printMatchingStrings(string array[], string text);

Algorithm

To solve the problem of finding and printing all strings from an array that occur as substrings in a given string, we can follow the following step-by-step algorithm −

  • Initialize an empty vector to store the matching strings.

  • Repeat over each string in the array.

  • Check in the event that the ongoing string is a substring of the given text.

  • Assuming it is, add the string to the vector of matching strings.

  • After iterating through all the strings, print the vector of matching strings.

Approach 1: Using the string.find() Function

In this technique, we will use the string.find() function, which returns the position of a substring inside a string. On the off chance that the substring isn't found, it returns an exceptional worth called string::npos.

Example

#include <iostream>
#include <vector>
#include <string>

void printMatchingStrings(const std::string array[], const std::string& text, int arraySize) {
   std::vector<std::string> matchingStrings;

   for (int i = 0; i < arraySize; i++) {
      if (text.find(array[i]) != std::string::npos) {
         matchingStrings.push_back(array[i]);
      }
   }

   for (const std::string& match : matchingStrings) {
      std::cout << match << std::endl;
   }
}

int main() {
   const std::string array[] = { "apple", "banana", "orange", "pear" };
   const std::string text = "I like to eat bananas and oranges.";

   int arraySize = sizeof(array) / sizeof(array[0]);

   printMatchingStrings(array, text, arraySize);

   return 0;
}

Output

banana
orange

Approach 2: Using Regular Expressions

Regular Expression gives a powerful tool to pattern matching in strings. We can utilize them to tackle our problems also.

Example

#include <iostream>
#include <vector>
#include <string>
#include <regex>

void printMatchingStrings(const std::string array[], const std::string& text, int arraySize) {
   std::vector<std::string> matchingStrings;

   for (int i = 0; i < arraySize; i++) {
      std::regex pattern(array[i]);

      if (std::regex_search(text, pattern)) {
         matchingStrings.push_back(array[i]);
      }
   }

   for (const std::string& match : matchingStrings) {
      std::cout << match << std::endl;
   }
}

int main() {
   const std::string array[] = { "apple", "banana", "orange", "pear" };
   const std::string text = "I like to eat bananas and pear.";

   int arraySize = sizeof(array) / sizeof(array[0]);

   printMatchingStrings(array, text, arraySize);

   return 0;
}

Output

banana
pear

Choosing the Right Approach

The choice between the two approaches depends on the requirements of your specific problem −

Use the string.find() approach if

The patterns to be matched are relatively simple.

Performance is a concern, as the string.find() approach can be faster than regular expressions for simple patterns.

You prefer a simpler implementation without the need for regular expression syntax.

Use the regular expressions approach if

The patterns to be matched are complex and require advanced pattern matching features.

Flexibility and power in pattern matching are important.

Performance is not a critical factor or the complexity of the patterns justifies the use of regular expressions.

Conclusion

In this article, we explored two unique ways to deal with the problem of finding and printing all strings from an array that occur as substrings in a given string. The main methodology used the string.find() function, which is a simple and straightforward solution. The subsequent methodology utilized the power of regular expression to deal with more complex pattern matching situations. Depending upon the necessities of your particular issue, you can pick the most suitable approach.Keep in mind, pattern matching is a fundamental task in programming, and having a strong comprehension of various methodologies and strategies can significantly upgrade your critical thinking abilities. So next time you go over a similar problem, you'll have the information to effectively handle it.

Updated on: 25-Jul-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements