« 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_MIN
11#include <unordered_map>
12using namespace std;
13
14bool helperpali(char input[], int start, int end)
15{
16 if (start > end)
17 {
18 return true;
19 }
20
21 if (input[start] != input[end])
22 {
23 return false;
24 }
25
26 return helperpali(input, start + 1, end - 1);
27}
28
29bool checkPalindrome(char input[])
30{
31 // Write your code here
32 int length = 0;
33 while (input[length] != '\0')
34 {
35 length++;
36 }
37
38 bool res = helperpali(input, 0, length - 1);
39
40 return res;
41}
42
43int main()
44{
45 char input[50];
46 cin >> input;
47
48 if (checkPalindrome(input))
49 {
50 cout << "true" << endl;
51 }
52 else
53 {
54 cout << "false" << endl;
55 }
56 return 0;
57}