I was working on potential interview questions, and one of them was to write a function in C to determine if a given string was a palindrome or not.
I got a good start:
#include <stdio.h>
#include <stdbool.h>
bool isPalindrome(char *value);
bool isPalindrome(char *value)
{
if (value == null)
return false;
char *begin = value;
char *end = begin + strlen(value) - 1;
while(*begin == *end)
{
if ((begin == end) || (begin+1 == end))
return true;
begin++;
end--;
}
return false;
}
int main()
{
printf("Enter a string: \n");
char text[25];
scanf("%s", text);
if (isPalindrome(text))
{
printf("That is a palindrome!\n");
}
else
{
printf("That is not a palindrome!\n");
}
}
However, now I want me to ignore spaces and punctuation marks.
What is the best way, given the code I wrote above, to move the pointers forward or backward if they run into punctuation / spaces?
source
share