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.
1) Input: txt = "THIS IS A TEST TEXT" pat = "TEST" Output: true 2) Input: txt = "geeksforgeeks" pat = "quiz" Output: false;
We strongly recommend to minimize the browser and try this yourself first.
Below is recursive algorithm.
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);
Below is C++ implementation of above algorithm.
1 0 1
This article is contributeed by Bhupinder. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Anagram Substring Search (Or Search for all permutations)
- C Program for Binary Search (Recursive and Iterative)
- Recursive program to linearly search an element in a given array
- Search an element in a Linked List (Iterative and Recursive)
- Recursive function to check if a string is palindrome
- Function to copy string (Iterative and Recursive)
- Recursive function to delete k-th node from linked list
- Find if a given string can be represented from a substring by iterating the substring “n” times
- Partition given string in such manner that i'th substring is sum of (i-1)'th and (i-2)'th substring
- Recursive and Recursive Enumerable Languages
- Print substring of a given string without using any string function and loop in C
- Unbounded Binary Search Example (Find the point where a monotonically increasing function becomes positive first time)
- Repeatedly search an element by doubling it after every successful search
- Binary Search Tree | Set 1 (Search and Insertion)
- Why is Binary Search preferred over Ternary Search?
Improved By : san123