
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Recursive function to do 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!
- Related Articles
- Binary Search (Recursive and Iterative) in C Program
- C Program for Anagram Substring Search
- C Program for Binary Search (Recursive and Iterative)?
- Recursive program to linearly search an element in a given array in C++
- MongoDB find() to operate on recursive search?
- Explain recursive function in C language with program
- Recursive function to check if a string is palindrome in C++
- Java Program for Binary Search (Recursive)
- Anagram Substring Search using Python
- C Program to reverse a given number using Recursive function
- C program to find GCD of numbers using recursive function
- Recursive Search and Replace in Text Files in Linux
- How to write a recursive function in Python?
- How to do recursive SELECT query in MySQL?
- C program to find GCD of numbers using non-recursive function
