Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!spool.mu.edu!agate!stanford.edu!leland.Stanford.EDU!dkeisen From: dkeisen@leland.Stanford.EDU (Dave Eisen) Newsgroups: comp.lang.c Subject: Re: How do you swap strings? Message-ID: <1991Jun2.133858.12882@leland.Stanford.EDU> Date: 2 Jun 91 13:38:58 GMT References: <1991Jun1.203738.28387@jack.sns.com> Distribution: na Organization: Sequoia Peripherals, Inc. Lines: 43 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