1#include <iostream>
2using namespace std;
3
4// Iterative function to check if given string is a palindrome or not
5bool isPalindrome(string str)
6{
7 int low = 0;
8 int high = str.length() - 1;
9
10 while (low < high)
11 {
12 // if mismatch happens
13 if (str[low] != str[high])
14 return false;
15
16 low++;
17 high--;
18 }
19
20 return true;
21}
22
23int main()
24{
25 string str = "XYXYX";
26
27 if (isPalindrome(str))
28 cout << "Palindrome";
29 else
30 cout << "Not Palindrome";
31
32 return 0;
33}
34
1#include <iostream>
2using namespace std;
3
4// Recursive function to check if str[low..high] is a palindrome or not
5bool isPalindrome(string str, int low, int high)
6{
7 // base case
8 if (low >= high)
9 return true;
10
11 // return false if mismatch happens
12 if (str[low] != str[high])
13 return false;
14
15 // move to next the pair
16 return isPalindrome(str, low + 1, high - 1);
17}
18
19int main()
20{
21 string str = "XYBYBYX";
22 int len = str.length();
23
24 if (isPalindrome(str, 0, len - 1))
25 cout << "Palindrome";
26 else
27 cout << "Not Palindrome";
28
29 return 0;
30}
31