Thread: swapping strings (which are stored in an array)

  1. #1
    Registered User
    Join Date
    Oct 2010

    swapping strings (which are stored in an array)

    I am new to programming, new to the C language, and new to this forum.

    My question deals with manipulating the elements of an array which store strings, (strings being an array of chars). Specifically, I want to know how to swap the contents of one element (which contains a string) with the contents of another element (which contains a string).

    Say I define an array as follows:
    char stuff[2]={"Hello","World"};
    How can I swap the contents "Hello", found at stuff[0], with "World" found at stuff[1]?

    I am familiar with swapping elements in an array, doing something like:
    but I have only been able to do it successfully with arrays storing integers. When I try to do it with arrays storing strings, I run into problems. I have a shallow understanding of strings in C. I don't know if I am suppose to be using pointers, if I am suppose to be loading certain libraries like <string.h>, or if I should be using some other approach I am not even considering. I've got a shallow understanding of pointers too, and I've never even used the <string.h> library before. The K&R book's appendix explains some of the things I can do with <string.h>, but I haven't been able to apply those things to my problem.

    So far, strings have proved very difficult for me to manipulate. Integers seem so much friendlier in C.

  2. #2
    Join Date
    Aug 2010
    Ontario Canada
    It's the same basic procedure... copy to a temp, move one to the other, copy the temp back in, but you are doing it with strcpy() or wcscpy() instead of simple equals signs.

    Something like this...
    char stuff[2]={"Hello","World"};
    void Swap(void)
      { char temp[6];
         strcpy(stuff[1], stuff[2]);
         strcpy(stuff[2],temp); }
    The big reason strings seem "unfriendly" in C is that C does not natively support a "string" type. A C string is an array of characters.
    Last edited by CommonTater; 10-03-2010 at 06:19 PM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Toronto, Canada
    However, if you had
    char *stuff[2]={"Hello","World"};
    then you could swap the pointers via:
    char *temp;
    temp = array[0];
    array[0] = array[1];
    array[1] = temp;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Passing an Array of Strings from VB to a C DLL
    By mr_nice! in forum Windows Programming
    Replies: 9
    Last Post: 03-08-2005, 06:16 AM