« Check palindrome recursively
Problem
Check Palindrome (recursive)
Check whether a given String S is a palindrome using recursion. Return true or false.
Input Format :
String S
Output Format :
'true' or 'false'
Constraints :
0 <= |S| <= 1000
where |S| represents length of string S.
Sample Input 1 :
racecar
Sample Output 1:
true
Sample Input 2 :
ninja
Sample Output 2:
false
Solution:
1#include <iostream>2#include <iomanip>3#include <algorithm>4#include <string>5#include <cstring>6#include <vector>7#include <cmath>8#include <map>9#include <climits>10// climits for INT_MIN11#include <unordered_map>12using namespace std;1314bool helperpali(char input[], int start, int end)15{16 if (start > end)17 {18 return true;19 }2021 if (input[start] != input[end])22 {23 return false;24 }2526 return helperpali(input, start + 1, end - 1);27}2829bool checkPalindrome(char input[])30{31 // Write your code here32 int length = 0;33 while (input[length] != '\0')34 {35 length++;36 }3738 bool res = helperpali(input, 0, length - 1);3940 return res;41}4243int main()44{45 char input[50];46 cin >> input;4748 if (checkPalindrome(input))49 {50 cout << "true" << endl;51 }52 else53 {54 cout << "false" << endl;55 }56 return 0;57}