I've heard that, in some job interviews, people have had to write a function that determines whether a string is a Palindrome. I don't know whether this is true, but I thought it would be a good practice exercise. My C function operates on the following assumptions:
'-:;, .?!
ABCDcba should be considered a palindrome.
I wrote a helper function called ignorable() to determine whether a given character should be considered or ignored. It is defined as follows:
short ignorable(char c)
{
char* ignorable_char = "'-:;, .?!";
int i;
for (i=0; i < 10; i++)
{
if (c == ignorable_char[i])
{
return 1;
}
}
return 0;
}
Here's the palindrome function itself:
short palindrome(char* str)
{
int last = strlen(str) - 1;
char* m = str;
char* p = &str[last];
while (m < p)
{
if (ignorable(*m))
{
m++;
}
else if (ignorable(*p))
{
p--;
}
else if (toupper(*m++) != toupper(*p--))
{
return 0;
}
}
return 1;
}
I'm Stephen Van Dahm, a software developer from Minneapolis, Minnesota.
Graphic design is hard! Let's go shopping!
After reading James Bennett's latest post, I immediately tried the following Unix command....
I don't know what the folks at Apple were smoking when they decided to build that ugly, glassy fake 3D Dock into Leopard. There is no way in hell that I'm going to look at that every day for the next two years. Fortunately, there appears to be a solution.
Read every post I've ever written, because I'm such an interesting guy.