[comp.lang.c] How do you swap strings?

jtanner@jack.sns.com (Jason Tanner) (06/02/91)

  Is there a command to swap to elements in an array?
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..

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

dkeisen@leland.Stanford.EDU (Dave Eisen) (06/02/91)

In article <1991Jun1.203738.28387@jack.sns.com> jtanner@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@jack.sns.com   -  -   Coke IS it!   - -    Knowledge = Power!!   |
>|                This space was intentionally left filled.                   |
>|------------------------------Dare to think!--------------------------------|


-- 
Dave Eisen                           dkeisen@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

glenn@zeus.ocs.com (Glenn Ford) (06/05/91)

In article <1991Jun1.203738.28387@jack.sns.com> jtanner@jack.sns.com (Jason Tanner) writes:
>
>  Is there a command to swap to elements in an array?
>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);
>



not good, especially if used alot. add this:
char *ptr_temp;
char *ptr_dest;
char *ptr_source;

ptr_temp = ptr_dest;
ptr_dest = ptr_source;
ptr_source = ptr_temp;

working with pointers to strings is MUCH faster than using just
a regular string.  Especially for string manipulation..

Glenn Ford
glenn@zeus.ocs.com
..uunet!ocsmd!glenn