
- 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 check if a string is palindrome in C++
We are given a string Str as input. The goal is to find if the input string is a palindrome word or not using a recursive function. Palindrome strings are those strings that when read from front or end form the same word. The strings of length 0 are considered as palindromes. Reversing the palindromes character wise forms, the same string as the original.
Examples of palindromes are:- madam, abcba, malayalam etc
Examples
Input − Str = “malayalam”
Output − Input string is palindrome.
Explanation −
Str[ 0 to 8 ] = malayalam
Reverse Str [ 8 to 0 ] = malayalam
Both strings are same.
Input − Str = “tutorial”
Output − Input string is not a palindrome.
Explanation −
Str[ 0 to 7 ] = tutorial
Reverse Str [ 7 to 0 ] = lairotut
Both strings are different
Approach used in the below program is as follows
In this approach we will check if the string contains a single character, if true then it is palindrome. If not, then recursively traverse the whole string for remaining characters and break the recursion if corresponding characters are different.
Take the input string Str[] and calculate its length.
If length is 0 then set result=1.
Else set result=checkPalindrome(Str, 0, length - 1) where 0 is first index and lenght - 1 is last index
Function checkPalindrome(char str[], int first, int last) returns 0 if any character does not match with its corresponding character in the string.
If indexes first and last are the same then string has one character, then return 1.
If not then check the remaining characters except end characters by first++, last-- and recursive call checkPalindrome(str, first, last).
At the end of all recursions we will get a result.
If it is 1 then the input string is palindrome.
Else input string is not a palindrome.
Print result in main.
Example
#include <bits/stdc++.h> using namespace std; int checkPalindrome(char str[], int first, int last){ if (first < last + 1){ first++; last--; return checkPalindrome(str, first, last); } if (first == last){ return 1; } if (str[first] != str[last]){ return 0; } return 1; } // Driver Code int main(){ char Str[] = "madam"; int result; int length = strlen(Str); if (length == 0){ result=1; } else{ result=checkPalindrome(Str, 0, length - 1); } if (result==1){ cout << "Input string is palindrome."; } else{ cout << "Input string is not a palindrome."; } return 0; }
Output
If we run the above code it will generate the following Output
Input string is palindrome.
- Related Articles
- Recursive program to check if number is palindrome or not in C++
- Check if a string is palindrome in C using pointers
- C Program to Check if a Given String is a Palindrome?
- How to check if String is Palindrome using C#?
- C# program to check if a string is palindrome or not
- Check if a given string is a rotation of a palindrome in C++
- Check if a number is Palindrome in C++
- Python program to check if a string is palindrome or not
- Python program to check if a given string is number Palindrome
- JavaScript - Find if string is a palindrome (Check for punctuation)
- TCP Client-Server Program to Check if a Given String is a Palindrome
- How to check if a string is palindrome or not without filters in VueJs?
- Python program to check if the given string is vowel Palindrome
- C# program to check if binary representation is palindrome
- Check if any anagram of a string is palindrome or not in Python
