
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement String Matching Using Vectors
This is another string matching method. In this approach, we are searching for a substring using vectors.
In C++ we can create vectors easily using the standard library. We are taking the main string and the string that will be searched as a vector, then searching it into the main string. When one match is found the function returns the address, and removes it from the main string. So in the next iteration, it starts from location 0 and searches again.
For multiple occurrences, we are using loops and repeatedly searching for the match, and return the position.
Input: Main String: “ABAAABCDBBABCDDEBCABC”, Pattern “ABC” Output: Pattern found at position: 4 Pattern found at position: 10 Pattern found at position: 18
Algorithm
vector_pattern_search(main, substr)
Input − The main text and the substring.
Output − location where patterns are found
Begin p := starting point of the main string while r is not at the end of substr and p is not at the end of main, do r := starting of substr while item at position p & r are not same, and p in main, do p := p + 1 i := i + 1 done q := p while item at pos p & r are same, and r in substr and p in main, do p := p + 1 i := i + 1 r := r + 1 done if r exceeds the substr, then delete first occurrence of substr from main return the position where substr is found if p exceeds main string, then return 0 q := q + 1 p := q done End
Example Code
#include <iostream> #include <string> #include <vector> using namespace std; void take_string(vector<char> &string){ char c; while(true){ c = getchar(); if(c == '\n'){ break; } string.push_back(c); } } void display(vector<char> string){ for(int i = 0; i<string.size(); i++){ cout << string[i]; } } int match_string(vector<char>& main, vector<char> substr){ vector<char>::iterator p,q, r; int i = 0; p = main.begin(); while (r <= substr.end() && p <= main.end()){ r = substr.begin(); while (*p != *r && p < main.end()){ p++; i++; } q = p; while (*p == *r && r <= substr.end() && p<=main.end()){ p++; i++; r++; } if (r >= substr.end()){ main.erase(main.begin(), q + 1); return (i - substr.size() + 1); } if (p >= main.end()) return 0; p = ++q; } }
Output
Enter main String: C++ is programming language. It is object oriented language Enter substring to find: language Match found at Position = 20 Match found at Position = 52
- Related Articles
- C++ Program to Implement Bitap Algorithm for String Matching
- C++ Program to Perform String Matching Using String Library
- C++ Program to Implement Wagner and Fisher Algorithm for online String Matching
- Program to count items matching a rule using Python
- How to implement FLANN based feature matching in OpenCV Python?
- C++ Program to Implement Queue using Array
- C++ Program to Implement Stack using array
- Python Program to Implement Queues using Stacks
- How to perform string matching in MySQL?
- How to print string vectors vertically in R?
- C++ Program to Implement Queue using Linked List
- C++ Program to Implement Stack using linked list
- C++ Program to Implement Quick Sort Using Randomization
- C++ Program to Implement Queue Using Two Stacks
- C++ Program to Implement Stack Using Two Queues

Advertisements