int palindrome( char *string ); which returns 1 if the argument string is a pali
ID: 3614487 • Letter: I
Question
int palindrome( char *string );
which returns 1 if the argument string is a palindrome
and 0 otherwise. A palindrome is a string that reads
the same forwards and backwards. The function
should ignore all non-alphabetic characters, and
character comparisons should be case independent.
Some palindromes (courtesy of the web):
Able was I ere I saw Elba.
A man, a plan, a canal: Panama!
Do geese see God?
Kay, a red nude, peeped under a yak.
Madam, I’m Adam.
Murder for a jar of red rum.
Sit on a potato pan, Otis!
Document your program nicely, use sensible layout and indenting,use sensible variable
names, and test thoroughly. Call your file palindrome.c. Turn inyour program using
the WebCT assignments tool. I will grade it by cutting yourfunction and pasting it into
my testing program.
Write this using pointers. Start out two pointers, one on eachend of the string and move
them toward each other, skipping over spaces and punctuation,and comparing
characters as they are encountered. If the pointers meet in themiddle, then the string is
a palindrome. There are two ways that the pointers can meet— one way for odd sized
strings and another for even sized strings.
The assignment is to write a function, as described above, butyou probably will want to
write a complete program that tests your function. But be surethat your function
matches the specifications and does not do its own input oroutput.
Explanation / Answer
#include<stdio.h>
#include<conio.h>
#include <string.h>
int isPalindrome( char* );
int main()
{char input[40]={"A man, a plan, a canal: Panama!"};
int palin;
palin=isPalindrome(input);
if(palin==1)
printf(" is a Palindrome ");
else
printf(" is not a Palindrome ");
getch();
return 0;
}
int isPalindrome( char* input)
{int i=0;
int y=0;
char *end=input+strlen(input);
while(input<=end)
{while(!isalpha(*input))
input++;
while(!isalpha(*end))
end--;
if(toupper(*input)!=toupper(*end))
return 0;
input++;
end--;
}
return 1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.