A palindrome is a string that reads the same from the left and from the right. For example,
Example 1: Write a function that tests whether a string is a palindrome
def palindrome_check(string):
string = list(string)
tmp = []
for x in range(0, len(string)):
if(string[x] != " "):
tmp.append(string[x].lower())
array1 = []
i = 0
j = len(tmp)-1
while(i < len(tmp)):
array1.append(tmp[j])
i += 1
j -= 1
counter = 0
for x in range(0, len(tmp)):
if(tmp[x] == array1[x]):
counter += 1
if(counter == len(tmp)):
return True
return False
Example 2: reads the string in then determines if the string is a palindrome.
using namespace std;
// Recursive function to check if str[low..high] is a palindrome or not
bool isPalindrome(string str, int low, int high)
{
// base case
if (low >= high)
return true;
// return false if mismatch happens
if (str[low] != str[high])
return false;
// move to next the pair
return isPalindrome(str, low + 1, high - 1);
}
int main()
{
string str = "XYBYBYX";
int len = str.length();
if (isPalindrome(str, 0, len - 1))
cout << "Palindrome";
else
cout << "Not Palindrome";
return 0;
}