Given the string, check if it is a palindrome. code example

Example 1: check if a string is palindrome cpp

// Check whether the string is a palindrome or not.
#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: Write a function that tests whether a string is a palindrome

def palindrome_check(string):
    string = list(string)
    tmp = []
    #remove any spaces
    for x in range(0, len(string)):
        if(string[x] != " "):
            tmp.append(string[x].lower())
    
            
    #now reverse the string
    array1 = []
    i = 0
    j = len(tmp)-1

    while(i < len(tmp)):
        array1.append(tmp[j])
        i += 1
        j -= 1

    #check if array1 is equal to the string
    counter = 0
    for x in range(0, len(tmp)):
        if(tmp[x] == array1[x]):
            counter += 1

    #if the counter is equal to the length of the string then the word
    #is the same
    if(counter == len(tmp)):
        return True
    
    return False

Example 3: check if palindrome

function isPalindrome(str) {
  str = str.toLowerCase();
  return str === str.split("").reverse().join("");
}

Example 4: find all the palindrome substring in a given string

#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;

// expand in both directions of low and high to find all palindromes
void expand(string str, int low, int high, auto &set)
{
	// run till str[low.high] is a palindrome
	while (low >= 0 && high < str.length()
			&& str[low] == str[high])
	{
		// push all palindromes into the set
		set.insert(str.substr(low, high - low + 1));

		// expand in both directions
		low--, high++;
	}
}

// Function to find all unique palindromic substrings of given string
void allPalindromicSubstrings(string str)
{
	// create an empty set to store all unique palindromic substrings
	unordered_set<string> set;

	for (int i = 0; i < str.length(); i++)
	{
		// find all odd length palindrome with str[i] as mid point
		expand(str, i, i, set);

		// find all even length palindrome with str[i] and str[i+1] as
		// its mid points
		expand(str, i, i + 1, set);
	}

	// print all unique palindromic substrings
	for (auto i : set)
		cout << i << " ";
}

int main()
{
	string str = "google";

	allPalindromicSubstrings(str);

	return 0;
}

Example 5: 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 6: palindrome words python

mes=input("Enter the word and see if it is palindrome ")
if mes==mes[::-1]:
    print("This word is palindrome")
else:
    print("This word is not palindrome")