write a function that determines if a string is a palindrome code example
Example 1: check if a string is palindrome cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
int l = 0;
int h = s.length()-1;
while(h > l){
if(s[l++] != s[h--]){
cout << "Not a palindrome" << endl;
return 0;
}
}
cout << "Is a palindrome" << endl;
return 0;
}
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, 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;
}