As an exercise, I created the following program:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
int main(void)
{
char message[MAX];
char *p = message;
char *q = message;
char c = ' ';
printf("Enter a message: ");
while((c = toupper(getchar())) != '\n' && (p < message + MAX)) {
if(isalpha(c))
*p++ = c;
}
p--;
while(*p == *q) {
if((p == q) || (p == q - 1) || (p - 1 == q)) {
printf("Palindrome\n");
exit(0);
}
q++;
p--;
}
printf("Not a palindrome\n");
return 0;
}
Is it a problem/vulnerability, the fact that in the first while
loop, when entering for the last time, p
is pointing outside of the message[100]
array?
I decrement it below, but I am wondering if it is considered a bad practice to have it behave this way.