Write a program that reads a phrase entered by the user into an array. It indica
ID: 3692091 • Letter: W
Question
Write a program that reads a phrase entered by the user into an array. It indicates whether or not the phrase is a palindrome and for each pair of letters that are compared, it indicates whether or not they matched.
You should not use the stack/queue method for determining whether or not it is a palindrome. Instead, use two pointers into the array and compare pairs of elements.
Your program is not allowed to use indexes into the array in the executable part of the program. You must use only pointers. You do need a size as an index when declaring the array.
The user will terminate the phrase with an *.
Enter the string followed by an *: rise to vote sir*
Front Element: r Rear Element: r Matched
Front Element: i Rear Element: i Matched
Front Element: s Rear Element: s Matched
Front Element: e Rear Element: e Matched
Front Element: t Rear Element: t Matched
Front Element: o Rear Element: o Matched
'risetovotesir' is a palindrome!
Enter the string followed by an *: doomed mood*
Front Element: d Rear Element: d Matched
Front Element: o Rear Element: o Matched
Front Element: o Rear Element: o Matched
Front Element: m Rear Element: m Matched
Front Element: e Rear Element: d Mismatch
'doomedmood' is not a palindrome!
Enter the string followed by an *: time to say goodybye*
Front Element: t Rear Element: e Mismatch
Front Element: i Rear Element: y Mismatch
Front Element: m Rear Element: b Mismatch
Front Element: e Rear Element: y Mismatch
Front Element: t Rear Element: d Mismatch
Front Element: o Rear Element: o Matched
Front Element: s Rear Element: o Mismatch
Front Element: a Rear Element: g Mismatch
'timetosaygoodybye' is not a palindrome!
Explanation / Answer
#include<stdio.h>
#include<conio.h>
int main()
{
char str[30];
char *p,*t;
printf("Enter the string followed by an *: ");
gets(str);
for(p=str ; *p != '*' ; p++);
for(t=str, p-- ; p>=t; )
{
if(*p==*t)
{
printf(" Front Element: %c Rear Element: %c Matched",*p,*t);
p--;
t++;
}
else
{
printf(" Front Element: %c Rear Element: %c Mismatched",*p,*t);
break;
}
}
if(t>p)
printf(" String is palindrome");
else
printf(" String is Not palindrome");
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.