DEV Community

Argha
Argha

Posted on

Pattern presence verification using strings

Intro
Creating a plan for a program that searches whether a smaller string exists in another string.
It is assumed that the length of the smaller string would be less than the length of the larger string

Algorithm

Input Get the text string
Input Get the Pattern string
Process: Create patternExists variable and set to fault
Condition 1: Have we reached the end of the text string
    Process: get the first/next character of text string and store it in a variable called "char0"   
    Condition: have we reached the end of the pattern text
        Process: Get the char0+"index of pattern string" of text string and store it in variable "char1"
        Process: get the first/next character of pattern string and store it in variable "char2"
        Condition:Is char1 and char2 the same:
            Condition: Have we reached the end of the pattern string:
                Process: set patternExists varible to true
                Break 
            Condition Else:
                Break

        Condition Else:
            Process: set patternExists variable to false
            Break


Print the status of patternExists
Enter fullscreen mode Exit fullscreen mode

Flow Chart
Image description

Pseudocode

text= get input
pattern =get input
patternExists= False
While end of text no reached (index using i)
    char0= character at current index number
    While end of pattern text is not reached (index using j)
        char1=text[i+j]
        char2=pattern[j]
        if char1==char2
            patternExists=True    
        else
            patternExists=False
            break
    if patternExists==True
        break
if patternExists==true
    print pattern exists in text
else
    print pattern does not exist in text
Enter fullscreen mode Exit fullscreen mode

Conclusion
The code suggested works efficiently as it breaks away from loops when the pattern is identified and reduces
redundancy as it breaks away when the pattern mismatches.

Top comments (0)