program to check palindrome string code 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;
// Iterative function to check if given string is a palindrome or not
bool isPalindrome(string str)
{
int low = 0;
int high = str.length() - 1;
while (low < high)
{
// if mismatch happens
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: Write a program to check whether inputted string is palindrome or not.
int main()
{
char s[1000];
int i,n,c=0;
printf("Enter the string : ");
gets(s);
n=strlen(s);
for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
printf("string is palindrome");
else
printf("string is not palindrome");
return 0;
}