From 6a771b52c9dc1cc5426fc37069b336accb1371e3 Mon Sep 17 00:00:00 2001 From: Dhruv Garg <91418396+DhruvGarg9136@users.noreply.github.com> Date: Wed, 13 Oct 2021 17:58:18 +0530 Subject: [PATCH] Palindrome Create a C++ program to check if a string is a palindrome or not, using the recursion function. --- Data Structure and Algorithm/Palindrome.cpp | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Data Structure and Algorithm/Palindrome.cpp diff --git a/Data Structure and Algorithm/Palindrome.cpp b/Data Structure and Algorithm/Palindrome.cpp new file mode 100644 index 0000000..03a4192 --- /dev/null +++ b/Data Structure and Algorithm/Palindrome.cpp @@ -0,0 +1,34 @@ +#include +using namespace std; + +bool isPalRec(char str[],int s, int e) +{ + if (s == e) + return true; + + if (str[s] != str[e]) + return false; + + if (s < e + 1) + return isPalRec(str, s + 1, e - 1); + + return true; +} + +bool isPalindrome(char str[]) +{ + int n = strlen(str); + if (n == 0) + return true; + return isPalRec(str, 0, n - 1); +} + +int main() +{ + char str[] = "geeg"; + if (isPalindrome(str)) + cout << "Yes"; + else + cout << "No"; + return 0; +}