Efficient strcat implementation.

M

Mahesh

Hey Ben,

Sory about that. But thanks for your great help :).

Ben said:
Mahesh said:
I am looking forefficientstringcancatination c code. I did it using
ptr [using default method] but my code i think is notefficient.

One possible implementation:
strcpy(strchr(dst, '\0'), src);

But here is what the GNU libc manual says about strcat:

Thanks a lot Chris and all who assisted me in this regard.

"Chris"? My name is Ben, thanks.
 
S

santosh

Mahesh wrote:

TOP-POST REFORMATTED.

[ ... ]
Santosh,

*(temp++) = ' ' this is for inserting space between words so it
looks neat in final result.

Thanks :).

Okay, but usually it's not the job of a library function like strcat to
change the characters in the strings. Sometimes you may want a space
and sometimes you may not (as there may already be a space in one of
the strings).
 
A

arnuld

What would be the difference between

while(*temp++ = *src++)

vs

for( ; *temp = *src; temp++, src++)


nothing, of course

:)

I prefer the 1st style but if you are copying arrays I better use strcpy.
 
B

Barry Schwarz

nothing, of course
Did you mean that since both are missing the terminal semicolon they
would both wreck havoc on whatever the next statement happened to be?
Or did you not recognize that both pointers in the for loop end up
pointing one byte past where they end up in the while loop?


Remove del for email
 
B

Ben Bacarisse

Barry Schwarz said:
Did you mean that since both are missing the terminal semicolon they
would both wreck havoc on whatever the next statement happened to
be?

Good point.
Or did you not recognize that both pointers in the for loop end up
pointing one byte past where they end up in the while loop?

It's the other way round, as I am sure you know.
 
R

Richard

CBFalconer said:
Mahesh wrote: *** and top-posted. Fixed. ***

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. I fixed this one. See the following links:

<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)

Please stop breaking the rules with your signature.

The recommended length is 4 lines.

Your constant net nannying is tedious to the extreme. If you have nothing
to offer then please do not post. This is not your news group and your
constant off topic rambles and incorrect advice is liable to be a
detriment to the charter which is to discuss the C programming language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top