L
lovecreatesbeauty
Hello experts,
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.
Sincerely,
lovecreatesbeauty
/* 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 != s[len - 1 - i])
{
palindromic = false;
break;
}
}
}
return palindromic;
}
/* test */
#include <stdio.h>
int main()
{
printf("%i\n", palindrome("deed"));
printf("%i\n", palindrome("deeds"));
}
/*
$ gcc -W -Wall -std=c99 -pedantic palindrome.c
$ ./a.out
1
0
$
*/
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.
Sincerely,
lovecreatesbeauty
/* 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 != s[len - 1 - i])
{
palindromic = false;
break;
}
}
}
return palindromic;
}
/* test */
#include <stdio.h>
int main()
{
printf("%i\n", palindrome("deed"));
printf("%i\n", palindrome("deeds"));
}
/*
$ gcc -W -Wall -std=c99 -pedantic palindrome.c
$ ./a.out
1
0
$
*/