info@thistimebd.com

Thursday 21st of November 03:38:57pm

A palindrome is a phrase that reads the same backward as forward, ignoring whitespace and punctuation. For example, “Madam, I m Adam” and “Are we not drawn onward, we few? Drawn onward to new era?” are palindromes.Write a program that will determine whether a string entered from the keyboard is a palindrome

A palindrome is a phrase that reads the same backward as forward, ignoring whitespace and punctuation. For example, “Madam, I m Adam” and “Are we not drawn onward, we few? Drawn onward to new era?” are palindromes.Write a program that will determine whether a string entered from the keyboard is a palindrome.

Solution:

#include<stdio.h>

#include<string.h>

#include<ctype.h>

int main()

{

char sentence[1000];

char sentence_chars[1000];

int j=0,length=0,i;


printf("Enter a sentence ");

gets(sentence);

for(i=0; i<strlen(sentence); i++)

if(isalpha(sentence[i]))

sentence_chars[j++]=tolower(sentence[i]);

sentence_chars[j]=; // use single quotation

length=strlen(sentence_chars);


for(i=0; i<length/2; i++)

if(sentence_chars[i]!=sentence_chars[length-1-i])

break;


if(i==length/2)

printf("palindrome");

else

printf("not palindrome");

}