-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathPalindrome.cpp
58 lines (46 loc) · 1.02 KB
/
Palindrome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// A recursive C++ program to
// check whether a given number
// is palindrome or not
#include <bits/stdc++.h>
using namespace std;
// A recursive function that
// check a str[s..e] is
// palindrome or not.
bool isPalRec(char str[],
int s, int e)
{
// If there is only one character
if (s == e)
return true;
// If first and last
// characters do not match
if (str[s] != str[e])
return false;
// If there are more than
// two characters, check if
// middle substring is also
// palindrome or not.
if (s < e + 1)
return isPalRec(str, s + 1, e - 1);
return true;
}
bool isPalindrome(char str[])
{
int n = strlen(str);
// An empty string is
// considered as palindrome
if (n == 0)
return true;
return isPalRec(str, 0, n - 1);
}
// Driver Code
int main()
{
char str[] = "geeg";
if (isPalindrome(str))
cout << "Yes";
else
cout << "No";
return 0;
}
// This code is contributed by Aditya DWivedi