"lovecreatesbea uty" <lo************ ***@gmail.com> writes:
I write a function named palindrome to determine if a character string
is palindromic, and test it with some example strings. Is it suitable
to add it to a company/project library as a small tool function
according to its quality? I will be very happy to get your suggestion
from every aspect on it: interface design, C language knowledge or
algorithm efficient.
/* Filename : palindrome.c
* Function : bool palindrome(char *s);
* Description: to determine if a character string is palindromic
* Date : 8 May 2006
*/
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
bool palindrome(char *s)
{
bool palindromic = true;
size_t len = strlen(s);
if (len > 1)
{
for (unsigned i = 0; i < len / 2; ++i)
{
if (s[i] != s[len - 1 - i])
{
palindromic = false;
break;
}
}
}
return palindromic;
}
/* test */
#include <stdio.h>
int main()
{
printf("%i\n", palindrome("dee d"));
printf("%i\n", palindrome("dee ds"));
}
Your use of <stdbool.h> and of a declaration within a for loop mean
your program will work only with a C99 implementation, or with an
implementation that provides these features as an extension. Only you
can decide whether that's a problem. Converting the program to C90,
or rather to the common subset of C90 and C99, is straightforward .
You use an unsigned object to iterate through the string, but the
upper bound of the iteration is of type size_t. It's conceivable that
size_t could be bigger than unsigned, and that your loop could fail
for a very long string. You're unlikely to run into this problem
either in practice or in testing, unless you're specifically looking
for it. On many implementations , you'll never see a problem (i.e.,
testing can never reveal the bug). In other words, this bug is
particularly insidious.
The printf statements invoke undefined behavior. Your palindrome()
function returns a bool; printf's "%i" expects an int.
Note that "%i" is equivalent to "%d" in a printf format string; "%d"
is much more common in my experience. Using "%i" is perfectly correct
(if the corresponding argument is an int), but seeing it forces me to
pause and wonder why the author chose to use "%i" rather than "%d".
Perhaps the author thought there was a difference, and is failing to
express something significant. This is a minor style issue; feel free
to ignore it. (Note that "%d" and "%i" do behave differently in
scanf.)
You haven't defined the term "palindromi c". The usual definition
ignores whitespace, punctuation, and the distinction between upper and
lower case; for example, "A man, a plan, a canal, Panama!" is
considered to be a palindrome. If you want to use a simpler
definition, that's fine, but you need to state clearly what definition
you're using.
Finally, the only use I've ever seen for a function to determine
whether a string is a palindrome is as an exercise in a programming
class or other tutorial. I can't imagine including such a function,
however well implemented, in a library, or calling it other than in a
test of the function itself.
--
Keith Thompson (The_Other_Keit h)
ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.