detect palindrome code example
Example 1: check if palindrome
function isPalindrome(str) {
str = str.toLowerCase();
return str === str.split("").reverse().join("");
}
Example 2: reads the string in then determines if the string is a palindrome.
#include <iostream>
using namespace std;
bool isPalindrome(string str)
{
int low = 0;
int high = str.length() - 1;
while (low < high)
{
if (str[low] != str[high])
return false;
low++;
high--;
}
return true;
}
int main()
{
string str = "XYXYX";
if (isPalindrome(str))
cout << "Palindrome";
else
cout << "Not Palindrome";
return 0;
}
Example 3: check palindrome
bool isPlaindrome(string s)
{
int i=0;
int j=s.length()-1;
while(i<j)
{
if(s[i]==s[j])
{i++;
j--;}
else break;
}
if (i==j || i>j) return 1;
else return 0;
}
Example 4: reads the string in then determines if the string is a palindrome.
#include <iostream>
using namespace std;
bool isPalindrome(string str, int low, int high)
{
if (low >= high)
return true;
if (str[low] != str[high])
return false;
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;
}