By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Largest Palindrome

P: n/a
HI Everybody,

I 've to submit a program on c.Can any one help me
plz.........The problem is like this::

Write a program which computes the largest palindrome substring of a
string.

Input:
The input is the string whose largest palindrome substring should be
found.

Output:
The output displays the largest palindrome substring.

Sample Input:
Enter the string: Habitat

Sample Output:
The longest palindrome substring of Habitat is: tat

Jul 11 '06 #1
Share this Question
Share on Google+
32 Replies


P: n/a
ra***************@gmail.com wrote:

[ Snip whole homework assignment written by a teacher who is clearly
better at non-d00d English than his pupils. ]

And your work so far is? Surely you don't expect us to do your homework
for you?

Richard
Jul 11 '06 #2

P: n/a
MQ

Richard Bos wrote:
ra***************@gmail.com wrote:

[ Snip whole homework assignment written by a teacher who is clearly
better at non-d00d English than his pupils. ]

And your work so far is? Surely you don't expect us to do your homework
for you?

Richard
This really annoys me. As if there isn't enough incompetent IT
professionals out there...

Jul 11 '06 #3

P: n/a
<ra***************@gmail.comwrote:

I 've to submit a program on c.Can any one help me
plz.........The problem is like this::

Write a program which computes the largest palindrome substring of a
string.

Input:
The input is the string whose largest palindrome substring should be
found.

Output:
The output displays the largest palindrome substring.

Sample Input:
Enter the string: Habitat

Sample Output:
The longest palindrome substring of Habitat is: tat
Note that the first letter of a palindrome is the same as the last letter.
Proceed from there.
Jul 11 '06 #4

P: n/a
HI Everybody,
>
I 've to submit a program on c.Can any one help me
plz.........The problem is like this::

Write a program which computes the largest palindrome substring of a
string.

Input:
The input is the string whose largest palindrome substring should be
found.

Output:
The output displays the largest palindrome substring.

Sample Input:
Enter the string: Habitat

Sample Output:
The longest palindrome substring of Habitat is: tat

I had this assignment a few years ago, got an A! Here's the exact code copy-
pasted. The code is perfect, fully-portable... just need to hand it in!

#include
"stdio.h"

#include
"string.h"

void main(int)
{
char[64] buffer;

scanf(STRING,buffer);

int * const p;

while
{
p = permute(buffer, buffer + len);
} do( p --p );
printf(p);
}
Hope that helps, always love to help out a fellow student : ) !
--

Frederick Gotham
Jul 11 '06 #5

P: n/a
On 2006-07-11, Frederick Gotham <fg*******@SPAM.comwrote:
I had this assignment a few years ago, got an A! Here's the exact code copy-
pasted. The code is perfect, fully-portable... just need to hand it in!

#include
"stdio.h"

#include
"string.h"

void main(int)
{
char[64] buffer;

scanf(STRING,buffer);

int * const p;

while
{
p = permute(buffer, buffer + len);
} do( p --p );

printf(p);
}

Hope that helps, always love to help out a fellow student : ) !
I laughed out loud at that one. :-)

--
Andrew Poelstra <http://www.wpsoftware.net/projects/>
To email me, use "apoelstra" at the above domain.
"You people hate mathematics." -- James Harris
Jul 11 '06 #6

P: n/a
Andrew Poelstra wrote:
>
On 2006-07-11, Frederick Gotham <fg*******@SPAM.comwrote:
I had this assignment a few years ago, got an A!
Here's the exact code copy-
pasted. The code is perfect, fully-portable...
just need to hand it in!

#include
"stdio.h"

#include
"string.h"

void main(int)
{
char[64] buffer;

scanf(STRING,buffer);

int * const p;

while
{
p = permute(buffer, buffer + len);
} do( p --p );

printf(p);
}

Hope that helps, always love to help out a fellow student : ) !

I laughed out loud at that one. :-)
Nobody is entitled to make any serious decisions
based on "code tested" and "code not tested" labels
on posted code.

Which is why
I'm not a fan of "code tested" and "code not tested" labels.

No criticism of Frederick Gotham is intended here.

--
pete
Jul 11 '06 #7

P: n/a
Andrew Poelstra wrote:
I laughed out loud at that one. :-)
I have written a function to identify a whole as palindromic or not. My
function knows this one is a palindrome:

A man, a plan, a canal - Panama!

To determine whether the following one is a palindrome will be more
difficult than the last one.

abcdefghijklmnopqrstuvwxyz0123456789 A man, a plan, a canal - Panama!

(please do not laugh at the following function:), but comments are
welcome!)

/*palindrome.c*/

#include <stddef.h>
#include <string.h>
#include <ctype.h>

/*determine a sentence if it is a palindrome. s: the string to be
determined. return 1 for identifying a successful palindrome, 0 for
not. - jhl, Jul 2006*/

int palindrome(const char *s)
{
int pln = 1;
const char *left, *right;

if (s != NULL){
left = s;
right = s + strlen(s) - 1;
while (left < right){
if (!isalnum(*left)){
++left;
continue;
}
if (!isalnum(*right)){
--right;
continue;
}

if (toupper(*left) == *right || tolower(*left) == *right){
++left;
--right;
continue;
} else {
pln = 0; /*charaters occur mismatched, it is not palindrome*/
break;
}
}
}

return pln;
}

/* test */
#include <stdio.h>
int main()
{
printf("%d\n", palindrome("Madam, I'm Adam!"));
printf("%d\n", palindrome("A man, a plan, a canal - Panama!"));
printf("%d\n", palindrome("Able was I, ere I saw Elba."));
printf("%d\n", palindrome("Ana voli Milovana"));
return 0;
}

$ cc -ansi -pedantic -Wall -W palindrome.c
$ ./a.out
1
1
1
1
$

lovecreatesbeauty

Jul 11 '06 #8

P: n/a
lovecreatesbeauty said:
Andrew Poelstra wrote:
>I laughed out loud at that one. :-)

I have written a function to identify a whole as palindromic or not.
Is it that time already? Here we go, then:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define p void
#define a sizeof
#define l char
#define i return
#define n int
#define d size_t
#define r if
#define o while
#define m else
#define e for
#define ob putchar
#define fu strlen
#define sc isalpha
#define at tolower
#define ed fgets
#define cp printf
#define ro strchr
#define gr main
#define am stdin
n c(l*s){n k=
0;r(s){d x
=fu (s)
;r(x>1)
{l*t=s,
*u=s+x-
1;k=1;o
(k&&t<u
){r(sc(
*t)){r(
sc(*u))

{r(at(
*t)!=at(
*u) ){k
=0; }m{
++t;--u;}}m{--
u;}}m{++t;}}}}
i k ;}n
gr( p){
l b [01
<<1 <<1

<<1
<<1
<<1
<<1
<<1
<<1
<<1
],*
z,*s=" adeil"
"mnoprst.\n";

n f[]={00,01<<1
<<1,(1<<1<<1<<1
)|(
1<<
1)|
1},
v[]
={0
,(1<<1<<1)|(1<<
1)|1,1<<1<<1<<1
,(1 <<1
<<01 <<1
)|(01 <<1
<<1 )} ,q[
]={ (1 <<1
)&1 ,1 <<1
>>1 ,1 >>1
,(1 << 1<<
1<< 1) |1,
1<< 01<<1

>>1>>1,(1<<1
<<1 )|(
1<< 1>>
1), 1<<
1<< 1,(
1<< 1<<
1)| (01
<<1 )|1
,01 <<1
,(1<<1<<1<<1
)|(1<<01),1
<<1 <<1
<<1 ,(1
<<1 <<1
)|1 <<1
,(1 <<1
)|1,(1<<
1<< 1<<
1)| (01
<<1 <<1
)|1 ,(1
<<1 <<1
<<1)|(1<<
1<< 1)|
(01 <<1
)}; d y
;o( ed(
b,a (b)
,am )){
{z= ro(
b,1 *1*
'\n');}if(

z){ *z=
00;} {cp(
"%s", b);}e
(y=0;1 *y<a f
/a( 1[f ]); y++
)ob (y[f][ s])
;r( !c(b ))e
(y= 0; y<a
v/a 1[v
];y ++)
ob(01*y[v][s]);e
(y=0/1*1/1<<1<<1
<<1
;y<
a q
/a 1[q]
;y++)ob
(y[
q][
s])
;}i 0>>1<<0>>1<<
1*1/(1<<1<<01);}


--
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)
Jul 11 '06 #9

P: n/a
Richard Heathfield said:
Is it that time already? Here we go, then:
<snip>

Not sure what happened to the formatting there. The original, with proper
formatting, can be found here:

http://www.cpax.org.uk/prg/portable/...s/obfusc/pal.c

--
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)
Jul 11 '06 #10

P: n/a
lovecreatesbeauty wrote:

<snip>
if (toupper(*left) == *right || tolower(*left) == *right){
I would replace the above with either
if (toupper(*left) == toupper(*right)){
or
if (tolower(*left) == tolower(*right)){

depending on my mood. Also there is the debate about whether left and
right should be pointers to unsigned char or whether you should cast the
dereferenced value to unsigned char. Personally, I would make them
pointers to unsigned char for this function.

<snip>
--
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
Jul 11 '06 #11

P: n/a
Flash Gordon wrote:
lovecreatesbeauty wrote:
if (toupper(*left) == *right || tolower(*left) == *right){
I would replace the above with either
if (toupper(*left) == toupper(*right)){
or
if (tolower(*left) == tolower(*right)){
I expect this one may save one additional function call.

if (toupper(*left) == *right || tolower(*left) == *right){
depending on my mood. Also there is the debate about whether left and
right should be pointers to unsigned char or whether you should cast the
dereferenced value to unsigned char. Personally, I would make them
pointers to unsigned char for this function.
int palindrome(const char *s)
{
/*...*/
left = s;
right = s + strlen(s) - 1;
/*...*/

left and right is the same type as parameter s's, i.e. char *, and char
may be signed or unsigned, it is undefined. The sign flag does not
matter, does it?

lovecreatesbeauty

Jul 11 '06 #12

P: n/a

Richard Heathfield wrote:
lovecreatesbeauty said:
Andrew Poelstra wrote:
I laughed out loud at that one. :-)
I have written a function to identify a whole as palindromic or not.

Is it that time already? Here we go, then:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define p void
#define a sizeof
#define l char
#define i return
#define n int
<snip>

I just meant a whole string (sentence), not a sub-string, not a file.
But you can read your files one by one into a string variable (it is
your duty) then invoke my function (through the interface), it may
works :)

lovecreatesbeauty

Jul 11 '06 #13

P: n/a
lovecreatesbeauty wrote:
Flash Gordon wrote:
>lovecreatesbeauty wrote:
>> if (toupper(*left) == *right || tolower(*left) == *right){
I would replace the above with either
if (toupper(*left) == toupper(*right)){
or
if (tolower(*left) == tolower(*right)){

I expect this one may save one additional function call.

if (toupper(*left) == *right || tolower(*left) == *right){
Or if toupper and tolower are implemented as efficient macros your
method might be slower. I merely expressed my preference, which IMHO
shows the intent better) I did not say your code was wrong.
>depending on my mood. Also there is the debate about whether left and
right should be pointers to unsigned char or whether you should cast the
dereferenced value to unsigned char. Personally, I would make them
pointers to unsigned char for this function.

int palindrome(const char *s)
{
/*...*/
left = s;
right = s + strlen(s) - 1;
/*...*/

left and right is the same type as parameter s's, i.e. char *, and char
may be signed or unsigned, it is undefined.
No, it's implementation defined.
The sign flag does not
matter, does it?
It does matter. See the recent exceedingly long thread titled:
What's the deal with the "toupper" family?
for all the gory details.
--
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
Jul 11 '06 #14

P: n/a
lovecreatesbeauty wrote:

[some lines snipped]
int palindrome(const char *s)
{
int pln = 1;
const char *left, *right;

if (s != NULL){
while (FOO){
BAZ;
if (BAR){
++left;
--right;
continue;
} else {
pln = 0; /*charaters occur mismatched, it is not palindrome*/
break;
}
}
}

return pln;
}
Question for Richard -- how can you prefer this to:

int palindrome(const char *s)
{
const char *left, *right;

if (s == NULL)
return 1;

for (;;)
{
if (!FOO)
return 1;

BAZ;

if (!BAR)
return 0;

++left;
--right;
}
}

Specifically, are you comfortable with most of the function
being at an extra level of indent; and all those continues
and breaks?

Jul 12 '06 #15

P: n/a
Old Wolf said:

<ghastly code snipped>
>
Question for Richard -- how can you prefer this to:
<ghastly code snipped>

I don't. I think they're both ghastly.

--
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)
Jul 12 '06 #16

P: n/a

Richard Heathfield wrote:
Old Wolf said:

<ghastly code snipped>

Question for Richard -- how can you prefer this to:
<ghastly code snipped>

I don't. I think they're both ghastly.
Hello Old Wolf and Richard, could you write some graceful versions of
palindromic function, or people may think you were / are on
windbaggery, they may be eager to read that code from both you. My
ghastly code quoted below first:

/*palindrome.c*/
#include <stddef.h>
#include <string.h>
#include <ctype.h>

/*determine a sentence if it is a palindrome. s: the string to be
determined. return 1 for identifying a successful palindrome, 0 for
not. - jhl, Jul 2006*/

int palindrome(const char *s){
int pln = 1;
const char *left, *right;

if (s != NULL){
left = s;
right = s + strlen(s) - 1;
while (left < right){
if (!isalnum(*left)){
++left;
continue;
}
if (!isalnum(*right)){
--right;
continue;
}

if (toupper(*left) == *right || tolower(*left) == *right){
++left;
--right;
continue;
} else {
/*charaters occur mismatched, the string is not a palindrome*/
pln = 0;
break;
}
}
}

return pln;
}

Jul 12 '06 #17

P: n/a
lovecreatesbeauty said:
>
Richard Heathfield wrote:
>Old Wolf said:

<ghastly code snipped>
>
Question for Richard -- how can you prefer this to:
<ghastly code snipped>

I don't. I think they're both ghastly.

Hello Old Wolf and Richard, could you write some graceful versions of
palindromic function,
Yes, I could.
or people may think you were / are on windbaggery,
That's great psychology. Why don't you just say something rude about my
mother?

--
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)
Jul 12 '06 #18

P: n/a
On 12 Jul 2006 02:39:49 -0700, "lovecreatesbeauty"
<lo***************@gmail.comwrote:
>Richard Heathfield wrote:
>Old Wolf said:

<ghastly code snipped>
>
Question for Richard -- how can you prefer this to:
<ghastly code snipped>

I don't. I think they're both ghastly.

Hello Old Wolf and Richard, could you write some graceful versions of
palindromic function, or people may think you were / are on
windbaggery, they may be eager to read that code from both you. My
ghastly code quoted below first:
<OT>
Yeah, and please do not forget to write donation to public domain
clause so that ramakrishnadeepak and I can leech copies with good
conscience; Ramakrishna to teacher and I as a raw material for
t3d_find_barray_Rbarray_SUBSTRING_n_LONGEST_PALIND ROME()
(note expressiveness in function name)

Just kidding, at least don't do it for me :) I see people sending all
the time all kinds of sleazy psycho enhanced challenges formulated in
couple simple words that take hours or even days to execute. For
example when I recently posted a data-mining algorithm link to
requester who wanted data-mining algorithms in C (who had gotten
nothing from anyone else), few days later there was "Your Uncle"
asking me to prove that I understand or not understand the algorithms
in a (good) link-page I gave...
<snip>
>I'd love to hear anything you have to say about this topic. I believe it has forensic dimensions. bfx
</snip>

Ehh, dream on 'Uncle'.
Juuso Hukkanen
www.tele3d.com
(to reply by e-mail set addresses month and year to correct)
"t3d programming language" and the structure of t3d function prototype
are trademarks of Juuso Hukkanen. (Currently discussing the
transfer of those to a major charity organization).
</OT>


Jul 12 '06 #19

P: n/a
ra***************@gmail.com wrote:
I 've to submit a program on c.Can any one help me
plz.........The problem is like this::

Write a program which computes the largest palindrome substring of a
string.

Input:
The input is the string whose largest palindrome substring should be
found.

Output:
The output displays the largest palindrome substring.

Sample Input:
Enter the string: Habitat

Sample Output:
The longest palindrome substring of Habitat is: tat
is it september already?

--
Nick Keighley

Jul 12 '06 #20

P: n/a
Nick Keighley wrote:
ra***************@gmail.com wrote:
(fx:snip)
>Sample Output:
The longest palindrome substring of Habitat is: tat

is it september already?
For the last ten years.

--
Chris "ish" Dollin
"Who are you? What do you want?" /Babylon 5/

Jul 12 '06 #21

P: n/a
lovecreatesbeauty posted:

Hello Old Wolf and Richard, could you write some graceful versions of
palindromic function

Here's how I'd probably go about it.

I'd start off with a function signature like:

int IsPalindrome(char const *p,
int (* const Equality)(char,char),
int (* const Immune)(char) )

I'd create a pointer to the start of the string, and a pointer to the end
of the string. I'd increment the former, and decrement the latter, each
time firstly checking if either char is immune from the test (e.g. non-
alphabetic), and then checking their equality.

The bare bones would be something like:
(Unchecked code, likely to contain errors)
#include <stddef.h>
#include <string.h>

int IsPalindrome(char const *p_start,
int (* const Equal)(char,char),
int (* const Immune)(char) )
{
size_t const len = strlen(p);

char const *p_end = p_start + (len - 1);

for( ; ; ++p_start, --p_end)
{
if(p_end - p_start <= 0) return 1;

while(Immune(*p_start)) ++p_start;
while(Immune(*p_end)) --p_end;

if(p_end - p_start <= 0) return 1;

if(!Equal(*p_start,*p_end)) return 0;
}
}
--

Frederick Gotham
Jul 12 '06 #22

P: n/a

In article <1d************@news.flash-gordon.me.uk>, Flash Gordon <sp**@flash-gordon.me.ukwrites:
lovecreatesbeauty wrote:
Flash Gordon wrote:
lovecreatesbeauty wrote:
if (toupper(*left) == *right || tolower(*left) == *right){
I would replace the above with either
if (toupper(*left) == toupper(*right)){
or
if (tolower(*left) == tolower(*right)){
I expect this one may save one additional function call.
If it does, it only saves it in the case where the first half of the
or-expression is true, ie if *right points to an uppercase letter
which is the same letter as pointed to by *left. In normal English
prose, uppercase letters are relatively infrequent, so the second
call would only be skipped in rather uncommon circumstances when
operating on normal input.

If it *is* skipped, on a typical modern general-purpose CPU, you're
likely to have a branch misprediction penalty, so there go any
savings.
Or if toupper and tolower are implemented as efficient macros your
method might be slower.
Indeed it might, since it introduces another branch.

More important, though, is that there's no evidence that this
"optimization" is at all justified. Is the runtime of this loop
crucial to any outside process? Who needs to shave a millisecond
off the time taken to find the palindrome?

So what we have is premature optimization, and probably adverse
optimization at that.

--
Michael Wojcik mi************@microfocus.com

Poe said that poetry was exact.
But pleasures are mechanical
and know beforehand what they want
and know exactly what they want. -- Elizabeth Bishop
Jul 12 '06 #23

P: n/a
Michael Wojcik wrote:
In article <1d************@news.flash-gordon.me.uk>, Flash Gordon <sp**@flash-gordon.me.ukwrites:
lovecreatesbeauty wrote:
Flash Gordon wrote:
>lovecreatesbeauty wrote:
>> if (toupper(*left) == *right || tolower(*left) == *right){
>I would replace the above with either
> if (toupper(*left) == toupper(*right)){
>or
> if (tolower(*left) == tolower(*right)){
>
I expect this one may save one additional function call.

If it does, it only saves it in the case where the first half of the
or-expression is true, ie if *right points to an uppercase letter
which is the same letter as pointed to by *left. In normal English
prose, uppercase letters are relatively infrequent, so the second
call would only be skipped in rather uncommon circumstances when
operating on normal input.
Yeah! It's more efficient after exchange those two function call, make
it as:

if (tolower(*left) == *right || toupper(*left) == *right){

/* the previous one was:
if (toupper(*left) == *right || tolower(*left) == *right){ */

Palindromes are rather common English (so the latest change may not be
more efficient), and no one knows what the largest palindrome will be.

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

lovecreatesbeauty

Jul 12 '06 #24

P: n/a

lovecreatesbeauty wrote:
>
/*palindrome.c*/

#include <stddef.h>
#include <string.h>
#include <ctype.h>

/*determine a sentence if it is a palindrome. s: the string to be
determined. return 1 for identifying a successful palindrome, 0 for
not. The code is mainly derived from Robert Gamble's code. - jhl, Jul 2006*/

int palindrome(const char *s)
{
int pln = 1;
const char *left, *right;

if (s != NULL){
left = s;
right = s + strlen(s) - 1;
while (left < right){
if (!isalnum(*left)){
++left;
continue;
}
if (!isalnum(*right)){
--right;
continue;
}

if (toupper(*left) == *right || tolower(*left) == *right){
++left;
--right;
continue;
} else {
pln = 0; /*charaters occur mismatched, it is not palindrome*/
break;
}
}
}

return pln;
}
The code is mainly derived from Robert Gamble's code. Thanks.

Jul 13 '06 #25

P: n/a

In article <11**********************@p79g2000cwp.googlegroups .com>, "lovecreatesbeauty" <lo***************@gmail.comwrites:
Michael Wojcik wrote:
lovecreatesbeauty wrote:
> if (toupper(*left) == *right || tolower(*left) == *right){

I expect this one may save one additional function call.
If it does, it only saves it in the case where the first half of the
or-expression is true, ie if *right points to an uppercase letter
which is the same letter as pointed to by *left. In normal English
prose, uppercase letters are relatively infrequent, so the second
call would only be skipped in rather uncommon circumstances when
operating on normal input.

Yeah! It's more efficient after exchange those two function call, make
it as:

if (tolower(*left) == *right || toupper(*left) == *right){
Well, there's a small lesson: when optimizing, concentrate on the
most common cases first, as a general rule.

But this was the least important of my points. As Flash originally
pointed out, on most implementations it's likely that:

if (tolower(*left) == tolower(*right))

is more efficient than either of your variations, as well as being
shorter and arguably clearer. (This style of coding may also be less
prone to error, as you only have to get the identifiers "left" and
"right" correct once.)

And *far* more important is the realization that *performance almost
certainly does not matter here*. Prefer instead to concentrate on
correctness and readability.

--
Michael Wojcik mi************@microfocus.com

Do not "test" parts, as this may compromise sensitive joinery. Those who
suffer difficulty should abandon the enterprise immediately. -- Chris Ware
Jul 14 '06 #26

P: n/a
Michael Wojcik wrote:
In article <11**********************@p79g2000cwp.googlegroups .com>, "lovecreatesbeauty" <lo***************@gmail.comwrites:
>Michael Wojcik wrote:
>>>>>lovecreatesbeauty wrote:
>> if (toupper(*left) == *right || tolower(*left) == *right){
I expect this one may save one additional function call.
If it does, it only saves it in the case where the first half of the
or-expression is true, ie if *right points to an uppercase letter
which is the same letter as pointed to by *left. In normal English
prose, uppercase letters are relatively infrequent, so the second
call would only be skipped in rather uncommon circumstances when
operating on normal input.
Yeah! It's more efficient after exchange those two function call, make
it as:

if (tolower(*left) == *right || toupper(*left) == *right){

Well, there's a small lesson: when optimizing, concentrate on the
most common cases first, as a general rule.

But this was the least important of my points. As Flash originally
pointed out, on most implementations it's likely that:

if (tolower(*left) == tolower(*right))

is more efficient than either of your variations,
There is another lesson as well. Optimisation really is for experts
only, because if you are not an expert on the specific implementation it
is quite easy for your supposed optimisation to actually produce code
that is almost certain to be slower (as with lovecreatsbeauty's original
suggestion).
as well as being
shorter and arguably clearer. (This style of coding may also be less
prone to error, as you only have to get the identifiers "left" and
"right" correct once.)

And *far* more important is the realization that *performance almost
certainly does not matter here*. Prefer instead to concentrate on
correctness and readability.
Even where performance does matter I would still concentrate on clarity,
correctness and maintainability first. If, having done that, I can't get
the required performance, I will look at what the compiler produces and
see if that suggests any optimisations (with one Pascal compiler,
replacing constant calculations with the *commented* calculated value
was a good optimisation since it did the calculation at run time!). When
that does not allow me to optimise it enough, I have stopped wasting
time trying to guess how to get the compiler to produce good enough code
and written the *small* (usually) critical section directly in
assembler, but I've only needed to do that with one small piece of code
I knew would be hard for any compiler to optimise on the DSP processor,
and one entire project where the processor was a real bitch for a
horrible processor and the hardware was a long way under specified.

Oh, and ensuring that where lots of large structures are passed around
they are passed by reference (this was Pascal, so passing a pointer to
the struct in C) rather than passing the large structures by value.
Having done that, I politely pointed out to my manager that the original
design was completely wrong, so even though I have more than doubled the
speed of the code (thus making it work) it should not have been anything
like that design in the first place. Then he told me that he had
originally designed it, which made me very glad I had been polite about
it! Another thing this taught me was the importance of designing
software so that when overloaded it degrades gracefully rather than
having the first overload cause a slow down leading to more overload
leading to more slow down leading very quickly to it all grinding to a
halt as soon as the critical threshold is exceeded. Admittedly this last
lesson is not appropriate in all instances, but it does often mean that
people will get possibly a little annoyed about some jerkiness instead
of extremely pissed off with it suddenly not working.
--
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
Jul 14 '06 #27

P: n/a
av
On 14 Jul 2006 23:01:02 +0200, Flash Gordon wrote:
>There is another lesson as well. Optimisation really is for experts
only, because if you are not an expert on the specific implementation it
is quite easy for your supposed optimisation to actually produce code
that is almost certain to be slower (as with lovecreatsbeauty's original
suggestion).
if "Optimisation" is for experts only, nobody could optimise anything.
Jul 15 '06 #28

P: n/a
av said:
On 14 Jul 2006 23:01:02 +0200, Flash Gordon wrote:
>>There is another lesson as well. Optimisation really is for experts
only, because if you are not an expert on the specific implementation it
is quite easy for your supposed optimisation to actually produce code
that is almost certain to be slower (as with lovecreatsbeauty's original
suggestion).

if "Optimisation" is for experts only, nobody could optimise anything.
Hold that thought. It will serve you well.

Until you realise that optimisation is a function of design rather than
programming, you're not ready. By the time that lesson sinks in, you're
probably well on the way to becoming an expert anyway.
--
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)
Jul 15 '06 #29

P: n/a
av <av@ala.awrote:
On 14 Jul 2006 23:01:02 +0200, Flash Gordon wrote:
There is another lesson as well. Optimisation really is for experts
only, because if you are not an expert on the specific implementation it
is quite easy for your supposed optimisation to actually produce code
that is almost certain to be slower (as with lovecreatsbeauty's original
suggestion).

if "Optimisation" is for experts only, nobody could optimise anything.
First rule of optimisation: Don't Do It.
Second rule of optimisation (for expert only): Don't Do It Yet.

HTH; HAND.

Richard
Jul 17 '06 #30

P: n/a
av
On Mon, 17 Jul 2006 06:36:58 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
>av <av@ala.awrote:
>On 14 Jul 2006 23:01:02 +0200, Flash Gordon wrote:
>There is another lesson as well. Optimisation really is for experts
only, because if you are not an expert on the specific implementation it
is quite easy for your supposed optimisation to actually produce code
that is almost certain to be slower (as with lovecreatsbeauty's original
suggestion).

if "Optimisation" is for experts only, nobody could optimise anything.

First rule of optimisation: Don't Do It.
Second rule of optimisation (for expert only): Don't Do It Yet.
noone become expert if not try to optimize
so if noone of not expert optimize there will be no expert
>HTH; HAND.

Richard
Jul 17 '06 #31

P: n/a
av posted:

noone become expert if not try to optimize
so if noone of not expert optimize there will be no expert

noone learn read and rite witout lerning alfabet & lerning gramar & how 2
spel.

--

Frederick Gotham
Jul 17 '06 #32

P: n/a
av <av@ala.awrites:
On Mon, 17 Jul 2006 06:36:58 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
>>av <av@ala.awrote:
>>On 14 Jul 2006 23:01:02 +0200, Flash Gordon wrote:
There is another lesson as well. Optimisation really is for experts
only, because if you are not an expert on the specific implementation it
is quite easy for your supposed optimisation to actually produce code
that is almost certain to be slower (as with lovecreatsbeauty's original
suggestion).

if "Optimisation" is for experts only, nobody could optimise anything.

First rule of optimisation: Don't Do It.
Second rule of optimisation (for expert only): Don't Do It Yet.

noone become expert if not try to optimize
so if noone of not expert optimize there will be no expert
Fortunately, real life doesn't work that way. There are plenty of
experts who are capable of optimizing code. If they're experts, they
know when not to do it.

--
Keith Thompson (The_Other_Keith) 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.
Jul 17 '06 #33

This discussion thread is closed

Replies have been disabled for this discussion.