
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Recursive Function for Substring Search in C++
Given two strings Str and subStr as input. The goal is to find whether text present in subStr exists in Str as substring or not. The string X is called a substring of Y if whole X is present in Y at least once. We will use a recursive approach to do this.
For Example
Input − Str = “tutorialspoint” subStr=”Point”
Output − Given string does not contain substring!
Explanation − The string Point is not substring of tutorialspoint
Input − Str = “globalization” subStr=”global”
Output − Given string contains substring!
Explanation − The string global is substring of globalization
Approach used in the below program is as follows
In this approach we check if subStr is substring of Str in a recursive manner. The steps for recursion will be:-
1. Pass both strings to a recursive function where pointers will point to current character positions of both strings
If string is ended but pattern has more characters left, then return 0 as pattern not found and we reached at the end of string.
If the current character is the last character in the pattern then it is found in string, return 1.
If both current characters are the same, then move both pointers to next positions.
If both current characters do not match, then move the pointer for string to the next position.
Take the input strings as character arrays Str and subStr.
Function match(char *str1, char *substr1) takes two substrings and returns 1 if substr1 and str1 are the same.
Both pointers point to characters present in strings, initially at starting positions.
If substr is empty, then return 0.
If both strings are empty, then also return 0.
If both current characters are equal then recursively check for next characters using match(str1 + 1, substr1 + 1)
Function checksubString(char *str2, char *substr2) takes both strings and returns 1 if substr2 is present in str2.
If current characters pointed by str2 and substr2 are the same then check if consecutive characters also match using match () function. If it returns 1 then return 1.
If reached the end of str2 then return 0.
Otherwise recursively check for next character of str2 using checksubString(str2 + 1, substr2);
If all conditions conditions fail then also recursively check using checksubString(str2 + 1, substr2);
Print result according to return value.
Example
#include<iostream> using namespace std; int match(char *str1, char *substr1){ if (*substr1 == '\0'){ return 1; } if (*str1 == '\0'){ if(*substr1 != '\0'){ return 0; } } if (*str1 == *substr1){ return match(str1 + 1, substr1 + 1); } return 0; } int checksubString(char *str2, char *substr2){ if (*str2 == *substr2){ if(match(str2, substr2)){ return 1; } } if (*str2 == '\0'){ return 0; } else{ return checksubString(str2 + 1, substr2); } return checksubString(str2 + 1, substr2); } int main(){ char Str[]="tutorialspoint"; char subStr[]="point"; if(checksubString(Str,subStr)==1){ cout << "Given string contains substring!"; } else{ cout << "Given string does not contain substring!"; } return 0; }
Output
If we run the above code it will generate the following Output
Given string contains substring!