473,791 Members | 3,186 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determine if a character string is palindromic

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,

lovecreatesbeau ty
/* 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"));
}

/*
$ gcc -W -Wall -std=c99 -pedantic palindrome.c
$ ./a.out
1
0
$
*/

May 8 '06
25 6552
Richard Heathfield wrote:
Simon Biber said:
Richard Heathfield wrote:
You might want to test it with these well-known palindromes:

"Madam, I'm Adam!"
"Able was I, ere I saw Elba."
"A man, a plan, a canal - Panama!"


They are palindromic sentences, not palindromic strings.

They are palindromes.


Sure, but the original program did not claim to detect palindromes. It
claimed to detect palindromic strings. :-)
void convert_sentenc e_to_string(cha r *destination, const char *source)
{
for(; *source; source++)
{
if(isalpha((uns igned char) *source))
*destination++ = toupper((unsign ed char) *source);
}
}


And when were you planning on fixing the bug? :-)


Apart from the failure to include <ctype.h>, was there a bug in my code?

Simon.
May 10 '06 #21
Simon Biber said:
Richard Heathfield wrote:
Simon Biber said:

void convert_sentenc e_to_string(cha r *destination, const char *source)
{
for(; *source; source++)
{
if(isalpha((uns igned char) *source))
*destination++ = toupper((unsign ed char) *source);
}
}


And when were you planning on fixing the bug? :-)


Apart from the failure to include <ctype.h>, was there a bug in my code?


Yep. Here's a hint. The name of the function gives the intent, but the code
does not meet that intent. You have forgotten something rather important,
as a result of which disaster could well strike. In fact, it's a terminal
error!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 10 '06 #22
Richard Heathfield wrote:
Simon Biber said:

Richard Heathfield wrote:
Simon Biber said:

void convert_sentenc e_to_string(cha r *destination, const char *source)
{
for(; *source; source++)
{
if(isalpha((uns igned char) *source))
*destination++ = toupper((unsign ed char) *source);
}
}

And when were you planning on fixing the bug? :-)


Apart from the failure to include <ctype.h>, was there a bug in my code?

Yep. Here's a hint. The name of the function gives the intent, but the code
does not meet that intent. You have forgotten something rather important,
as a result of which disaster could well strike. In fact, it's a terminal
error!


Ah, yes. I am getting sloppy!

Insert a line
*destination = 0;
as the last line of the function.

I will admit that I had not tested the function the first time I posted
it. I did test it after your comment about a bug, but unfortunately it
worked fine. There must have been a serendipitous null byte in my array.

--
Simon.
May 10 '06 #23
On 2006-05-09, Flash Gordon wrote:
Chris F.A. Johnson wrote:
On 2006-05-08, Keith Thompson wrote:

(Chris, just out of curiosity, why do you always indent everything you
write?


For the same reason that books have margins, and better books wider
margins.


My news reader provides such indentation sufficient unto my needs. I
could probably configure it to provide more if I so desired. This does
not require that you indent your text.
Actaully, I started doing it in order to distinguish between my
text and (shell) code. I like code set flush left so that no
fiddling is necessary when cutting and pasting it.


Personally I find shells don't have a problem with white space before
the commands.


It would if the command, intending IFS to contain a single newline,
is:

IFS='
'

I have the same problem if I have my newsreader do the indentation.

--
Chris F.A. Johnson, author <http://cfaj.freeshell. org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
May 10 '06 #24
"Chris F.A. Johnson" <cf********@gma il.com> writes:
On 2006-05-09, Flash Gordon wrote:
Chris F.A. Johnson wrote:
On 2006-05-08, Keith Thompson wrote:

(Chris, just out of curiosity, why do you always indent everything you
write?

For the same reason that books have margins, and better books wider
margins.


My news reader provides such indentation sufficient unto my needs. I
could probably configure it to provide more if I so desired. This does
not require that you indent your text.
Actaully, I started doing it in order to distinguish between my
text and (shell) code. I like code set flush left so that no
fiddling is necessary when cutting and pasting it.


Personally I find shells don't have a problem with white space before
the commands.


It would if the command, intending IFS to contain a single newline,
is:

IFS='
'

I have the same problem if I have my newsreader do the indentation.


Since you're the only person I know of who does this, it makes your
posts, and posts that quote them, slightly more difficult to read.
I suggest not indenting your text and finding some other way to mark
code.

It's not a huge deal, and I don't plan to bring it up again. Sorry
for the digression.

--
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.
May 10 '06 #25
Vladimir Oka wrote:

Apart from the fact that it doesn't work, it looks fairly good.
Did you consider upper/lower case? Try: "Ana voli Milovana".
Or, alternatively, you need to define "palindrome " more precisely.


After seeing this example, I'm curious; where are you from?

May 10 '06 #26

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
36088
by: Ren? M?hle | last post by:
I have a psp script with a procedure just to run an update on one table. The Problem occurs when I try to compile this script with pspload: ORA-20006: "frsys_updatereport.psp": compilation failed with the following errors. ORA-06502: PL/SQL: numeric or value error: character string buffer too small Here the whole script:
1
3474
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example, I know the 18th through 21st byte is an integer value representing the width of the bmp image. So far, I have been able to use fstream's seek, write, and read to pull out chucks of bytes and convert them to usable integers straight out of the binary file. It works well, but over the course of...
2
13214
by: jt | last post by:
Looking for an example how to convert and CString to an ASCII character string. Any examples on how to do this? Thank you, jt
0
2181
by: MLH | last post by:
Is an apostrophe a character of special significance to MySQL in a way that would cause "Bob's dog" to become translated into a 12-character string when typed into a MySQL memo field? If I type Bob's dog into an Access memo field, I get a string that is 9-characters long. When I read "Bob's dog" from a memo field in a MySQL table attacted to MS Access via MyODBC driver, it displays as "Bob's dog" - a twelve character string. the '...
2
9297
by: Roy Rodsson via .NET 247 | last post by:
Hi all! I am using a stored procedure in SQL2000 for retrieving fileinformations from a db. the table as an uniqueidentifier for the file information. The stored procedure has a variable called fileIds that is oftype varchar. I am putting in a comma separated string with ids (such as:'9B176B0C-CA03-49C9-A2E7-063038E7CF20','9B176B0C-CA03-49C9-A2E7-063038E7CF22','9B176B0C-CA03-49C9-A2E7-063038E7CF23') However, when I execute the sp via...
7
2515
by: Justin | last post by:
i need to build the unsigned character string: "PR0N\0Spam\0G1RLS\0Other\0Items\0\0\0" from the signed character string: "PR0N Spam G1RLS Other Items" Tokeninzing the character string is not a problem. I can't solve my concatenation problem. I've researched this topic extensively and I've found nothing to help. Failure resulted when I used memcpy,_mbscat, and various other methods. If anyone knows how to build a unsigned
5
4854
by: Karthik | last post by:
Hello! I am not a wizard in this area! Just need some help out in this. I am trying to convert bstr string to new character string. Here is the snippet of my code. **** Code Start**** GlobalInterfacePtr->GetName(bstr, bstrTNamePtr, &retVal);
5
9257
by: Paul Aspinall | last post by:
Hi I want to send an ASCII character string / stream to an IP address. I basically have 6 barcode printers, and a web interface. Depending on what is entered on the web page, will determine which printer the label is printed on (ie. which IP address the ASCII string / stream is sent to). How can I send an ASCII string / stream to an IP address??
8
11887
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
0
9517
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10156
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6776
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.