Recursive function to do substring search - GeeksQuiz
Given a text txt[] and a pattern pat[], write a recursive function "contains(char pat[], char txt[])" that returns true if pat[] is present in txt[], otherwise false.
Read full article from Recursive function to do substring search - GeeksQuiz
Given a text txt[] and a pattern pat[], write a recursive function "contains(char pat[], char txt[])" that returns true if pat[] is present in txt[], otherwise false.
contains(tex[], pat[]) 1) If current character is last character of text, but pat has more characters, return false. 2) Else If current character is last character of pattern, then return true 3) Else If current characters of pat and text match, then return contains(text + 1, pat + 1); 4) Else If current characters of pat and text don't match return contains(text + 1, pat);
ool
contains(
char
*text,
char
*pat)
{
// If last character of text reaches, but pat
// has more characters
if
(*text ==
'\0'
&& *pat !=
'\0'
)
return
false
;
// Else If last character of pattern reaches
if
(*pat ==
'\0'
)
return
true
;
// If current characts of pat and tex match
if
(*text == *pat)
return
contains(text + 1, pat + 1);
// If current characts of pat and tex don't match
return
contains(text + 1, pat);
}