Throw the whole thing in a loop and break from the loop if the user enters something that isn't valid:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLINE 256
int main(void)
{
char str[MAXLINE], rev[MAXLINE];
int i, length;
for ( ; ; ) {
/*--------------------------------------------
* Read string and determine length
*--------------------------------------------*/
printf("Enter string or EOF to quit: ");
if ( scanf ("%s", str) != 1 )
break;
length = strlen(str);
/*---------------------------------------------
* Build the reversed string and print it.
* Don't forget to add the terminating 0!
*---------------------------------------------*/
for (i=0; i<length; i++)
rev[length-i-1] = str[i];
rev[length] = 0;
printf("reverse string: %s\n\n", rev);
/*---------------------------
* Palindrome?
*---------------------------*/
if (strcmp (str,rev) == 0)
printf ("Palindrome!\n\n");
else
printf ("Not a Palindrome\n\n");
}
return 0;
}
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Offline 11,807 posts
since Sep 2004