Coders Packet

C++ implementation of Pattern searching in a string in an efficient way

By RANJITH RAMAMURTHY TEVNAN

This is a C++ code which helps in detecting a pattern in a given string in a very efficient way. It takes two strings as input. First input is the pattern and the next one is the string(Text).

The code takes two strings as input. First input is the pattern and the next one is the string. It returns the positions where the pattern occurs in the text. In cases where the pattern doesn't exist, It would return the appropriate message.

A header file is also included which could be integrated in one's code. A function call with the string and pattern passed as arguments would would return a vector with the positions of pattern in the text. An empty vector would be returned in case where the pattern doesn't exist.

The function goes by the name patterncheck(pattern,text,output in terminal bit). The first and second arguments are pattern and text respectively which are passed as a string.The 3rd argument is the output bit which when set as 1 would print the positions in terminal(and return the vector) and 0 would only return the vector.

The pattern checking code used Knuth Morris Pratt(KMP) Algorithm which does the task in O(n+m) time complexity while the basic Algorithm has O(n*m) complexity. n is the length of the text and m is the length of the pattern. 

Download project

Reviews Report

Submitted by RANJITH RAMAMURTHY TEVNAN (ranjith1604)

Download packets of source code on Coders Packet