1// Check whether the string is a palindrome or not.
2#include <bits/stdc++.h>
3
4using namespace std;
5
6int main(){
7 string s;
8 cin >> s;
9
10 int l = 0;
11 int h = s.length()-1;
12
13 while(h > l){
14 if(s[l++] != s[h--]){
15 cout << "Not a palindrome" << endl;
16 return 0;
17 }
18 }
19 cout << "Is a palindrome" << endl;
20 return 0;
21
22}
1def palindrome_check(string):
2 string = list(string)
3 tmp = []
4 #remove any spaces
5 for x in range(0, len(string)):
6 if(string[x] != " "):
7 tmp.append(string[x].lower())
8
9
10 #now reverse the string
11 array1 = []
12 i = 0
13 j = len(tmp)-1
14
15 while(i < len(tmp)):
16 array1.append(tmp[j])
17 i += 1
18 j -= 1
19
20 #check if array1 is equal to the string
21 counter = 0
22 for x in range(0, len(tmp)):
23 if(tmp[x] == array1[x]):
24 counter += 1
25
26 #if the counter is equal to the length of the string then the word
27 #is the same
28 if(counter == len(tmp)):
29 return True
30
31 return False
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 <stdio.h>
2#include <string.h>
3int main()
4{
5
6 char str[80];
7 int length;
8 printf("\nEnter a string to check if it's a palindrome: ");
9 scanf("%s", str); // string input
10 length = strlen(str); // finding the string length
11 int i, j, count;
12 for (i = 0, j = length - 1, count = 0; i < length; i++, j--)
13 {
14 // matching string first character with the string last character
15 if (str[i] == str[j])
16 {
17 count++; // if character match, increasing count by one.
18 }
19 }
20 if (count == length) // if count == length, then the string is a palindrome.
21 {
22 printf("'%s' is a palindrome.\n", str);
23 }
24 else // otherwise the string is not a palindrome.
25 {
26 printf("'%s' is not a palindrome.\n", str);
27 }
28 return 0;
29}
1#include <iostream>
2#include <string>
3#include <unordered_set>
4using namespace std;
5
6// expand in both directions of low and high to find all palindromes
7void expand(string str, int low, int high, auto &set)
8{
9 // run till str[low.high] is a palindrome
10 while (low >= 0 && high < str.length()
11 && str[low] == str[high])
12 {
13 // push all palindromes into the set
14 set.insert(str.substr(low, high - low + 1));
15
16 // expand in both directions
17 low--, high++;
18 }
19}
20
21// Function to find all unique palindromic substrings of given string
22void allPalindromicSubstrings(string str)
23{
24 // create an empty set to store all unique palindromic substrings
25 unordered_set<string> set;
26
27 for (int i = 0; i < str.length(); i++)
28 {
29 // find all odd length palindrome with str[i] as mid point
30 expand(str, i, i, set);
31
32 // find all even length palindrome with str[i] and str[i+1] as
33 // its mid points
34 expand(str, i, i + 1, set);
35 }
36
37 // print all unique palindromic substrings
38 for (auto i : set)
39 cout << i << " ";
40}
41
42int main()
43{
44 string str = "google";
45
46 allPalindromicSubstrings(str);
47
48 return 0;
49}
1function isPalindrome(str) {
2 str = str.toLowerCase();
3 return str === str.split("").reverse().join("");
4}