Sometimes interviewer asks the question to search a pattern in a given string. There are a lot of algorithms to find the pattern, later we will discuss all the algorithms in detail. Here I am using the Naive algorithm (not a smart algorithm) to search a Pattern.
Suppose a given source string is src[0..n-1] and a pattern is pat[0..m-1]. Here to search the pattern for a given string, we need to slide the pattern string (pat) over source string (src) one by one and check for the match. If the match is found, then slides by 1 again to check for subsequent matches.
Example,
Input: src[] = “How are you”
Patteren: pat[] = “are”
Output: Pattern found at index 4
Output: Pattern found at index 4.
need an explanation for this answer? contact us directly to get an explanation for this answer