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
In article <44************ ***@yahoo.com>,
CBFalconer <cb********@mai neline.net> wrote: /* null and empty strings are not palindromes */
Why on earth would you choose to not call the empty string a
palindrome? That would break such obvious invariants (indeed,
one might say definitions) as
palindrome(s) == (strcmp(s, reverse(s)) == 0)
-- Richard
On 2006-05-08, Keith Thompson wrote: 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.
I use one in my work (composing cryptic crosswords).
--
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
"Chris F.A. Johnson" <cf********@gma il.com> writes: On 2006-05-08, Keith Thompson wrote: 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.
I use one in my work (composing cryptic crosswords).
Ok, sometimes my imagination fails me.
(Chris, just out of curiosity, why do you always indent everything you
write? Indentation like that is sometimes used to denote blocks of
quoted text. IMHO it would be clearer if your text were
left-justified like everyone else's.)
--
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.
On 2006-05-08, Keith Thompson wrote: "Chris F.A. Johnson" <cf********@gma il.com> writes: On 2006-05-08, Keith Thompson wrote: 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. I use one in my work (composing cryptic crosswords).
Ok, sometimes my imagination fails me.
(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.
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.
Indentation like that is sometimes used to denote blocks of quoted text.
Newsreaders, IME, generally do not recognize indentation as
quoting.
IMHO it would be clearer if your text were left-justified like everyone else's.)
Left-justified? You mean flush-left and/or ragged right.
--
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
Richard Tobin wrote: CBFalconer <cb********@mai neline.net> wrote:
/* null and empty strings are not palindromes */
Why on earth would you choose to not call the empty string a palindrome? That would break such obvious invariants (indeed, one might say definitions) as
palindrome(s) == (strcmp(s, reverse(s)) == 0)
All I can say is I documented it. That has been lying about in my
junk directory for four years. To alter that, simply change 0 to 1
in one location.
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Chris F.A. Johnson wrote: On 2006-05-08, Keith Thompson wrote: "Chris F.A. Johnson" <cf********@gma il.com> writes: On 2006-05-08, Keith Thompson wrote: 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. I use one in my work (composing cryptic crosswords). Ok, sometimes my imagination fails me.
(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. Indentation like that is sometimes used to denote blocks of quoted text.
Newsreaders, IME, generally do not recognize indentation as quoting.
Agreed. IMHO it would be clearer if your text were left-justified like everyone else's.)
Left-justified? You mean flush-left and/or ragged right.
Yes, the normal is flush-left and ragged right. Since it is the norm
news readers can reformat it as well.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc
Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
"Chris F.A. Johnson" wrote: On 2006-05-08, Keith Thompson wrote:
.... snip ... (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.
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.
Indentation like that is sometimes used to denote blocks of quoted text.
Newsreaders, IME, generally do not recognize indentation as quoting.
IMHO it would be clearer if your text were left-justified like everyone else's.)
Left-justified? You mean flush-left and/or ragged right.
Flush left. Your technique is fine until it gets requoted, when it
tends to spread out to the right, and be resistant to reformatting.
--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
john wrote: Robert Gamble wrote: Robert Gamble wrote: return 1;
That should be return 0;
} } } return 0;
And that should be return 1;
Is there any particular reason that functions often behave this way? If they succeed, they return 0, which in C is false, and vice versa.
Hmm, while I was writing this it occurred to me that it can have something to do with error reporting. Different return values can indicate different errors. Is that the reason?
It follows from a tradition in UNIX, however there is also a sound
reason for it. Very often a function returns with the *quantity* of
something it is trying to count from a complex data structure. However,
the data structure may be detectably corrupted or in an ill-defined
state. So using one int return value you can follow the convention that
all negative values indicate errors, and all positive values means
success -- further that all the positive values indicate each possible
count, while different negative values can correspond to different kinds
of errors.
So for example, how many elements are in a vector? Normally, you would
just want to know the quantity of elements that that vector has -- every
possibility from 0 to INT_MAX (say) makes sense, has meaning and easily
corresponds to a successful return. But if the vector is corrupted
because it has been ininitialized, or if an entry has some bad contents
or something like that you probably want some detail to help you track
down what kind of error you have, so that you can debug it, or avoid
that kind of vector contents. If you are just debugging, its usually
convenient to return -__LINE__. You can still reserve the value -1 for
other kinds of deterministic errors (like out of memory), since its
generally not possible to have source code which can expand the macro
__LINE__ on the first line of your source.
--
Paul Hsieh http://www.pobox.com/~qed/ http://bstring.sf.net/
Richard Heathfield wrote: lovecreatesbeau ty said:
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. 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.
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);
}
}
--
Simon.
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.
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? :-)
--
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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:
|
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...
|
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
|
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 '...
|
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...
| |
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
|
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);
|
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??
|
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?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |