How do you swap strings?

Dave Eisen dkeisen at leland.Stanford.EDU
Sun Jun 2 23:38:58 AEST 1991


In article <1991Jun1.203738.28387 at jack.sns.com> jtanner at jack.sns.com (Jason Tanner) writes:
>
>  Is there a command to swap to elements in an array?

No there isn't. 

>Currently I am using strcpy and a temporary string to swap around the 2
>elements and this dosnt seem to work very well.. Swapping strings is
>such an important command I cant believe that it dosnt exist.. now I am using
>something like this..
>
>  strcpy(temp,destination);
>  strcpy(destination,source);
>  strcpy(source,temp);
>
> there must be something better..

I don't know what you mean "doesn't work very well." This should work fine
(assuming all of the strings involved have enough space allocated), albeit
a little slowly.

But, yes, there is something better.

If you are moving the strings in your array around, you are better off
using an array of pointers to the strings instead of an array of character 
arrays. Then instead of having to move the data around by using all of these
strcpy's, you can just move the pointers.



>
>-- 
>|----------------------------------------------------------------------------|
>|   jtanner at jack.sns.com   -  -   Coke IS it!   - -    Knowledge = Power!!   |
>|                This space was intentionally left filled.                   |
>|------------------------------Dare to think!--------------------------------|


-- 
Dave Eisen                           dkeisen at leland.Stanford.EDU
1101 San Antonio Road, Suite 102     (Gang-of-Four is being taken off the net)
Mountain View, CA 94043
(415) 967-5644



More information about the Comp.lang.c mailing list