Home > Article > Backend Development > Print out all strings in a given array that occur as substrings in a given string using C++
In the world of programming, there are many scenarios where we really want to look for specific patterns in larger text. A common task is to find and print every string in a given array that occurs as a substring within a given string. This seemingly basic problem can be solved using various methods, and in this article we will explore two of them. We provide a clear explanation of the syntax and algorithms used for each method and provide two complete executable code examples.
Before we introduce the methods, let us first understand the syntax we will use to solve this problem -
void printMatchingStrings(string array[], string text);
To solve the problem of finding and printing all strings that occur as substrings in a given string from an array, we can follow the following step-by-step algorithm -
Initialize an empty vector to store matching strings.
Repeat each string in the array.
Checks whether the current string is a substring of the given text.
The assumption is that, add a string to a vector of matching strings.
After iterating through all strings, print the vector of matching strings.
In this technique, we will use the string.find() function, which returns the position of a substring in a string. If the substring is not found, it returns a special value called string::npos.
#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; }
banana orange
Regular expressions provide powerful tools for pattern matching in strings. We can also use them to solve our problems.
#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; }
banana pear
Choosing between the two methods depends on the requirements of your specific problem −
The pattern that needs to be matched is relatively simple.
Performance is an issue because the string.find() method may be faster than regular expressions for simple patterns.
You prefer a simpler implementation that does not require regular expression syntax.
The pattern to be matched is complex and requires advanced pattern matching capabilities.
Flexibility and powerful pattern matching are important.
Performance is not a critical factor, or the complexity of the pattern justifies the use of regular expressions.
In this article, we explore two unique ways to deal with the problem of finding and printing out the occurrence of a substring in a given string. The main method uses the string.find() function, which is a simple and straightforward solution. Subsequent methods exploited the power of regular expressions to handle more complex pattern matching situations. Depending on the needs of your specific problem, you can choose the most appropriate method. Remember that pattern matching is a fundamental task in programming, and having a strong understanding of various methods and strategies can significantly improve your problem-solving abilities. So the next time you encounter a similar problem, you'll have enough knowledge to handle it effectively.
The above is the detailed content of Print out all strings in a given array that occur as substrings in a given string using C++. For more information, please follow other related articles on the PHP Chinese website!