swapping strings (which are stored in an array)

This is a discussion on swapping strings (which are stored in an array) within the C Programming forums, part of the General Programming Boards category; I am new to programming, new to the C language, and new to this forum. My question deals with manipulating ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    30

    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:
    Code:
    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:
    Code:
    temp=array[x];
    array[x]=array[y];
    array[y]=temp;
    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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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...
    Code:
    char stuff[2]={"Hello","World"};
    
    void Swap(void)
      { char temp[6];
         strcpy(temp,stuff[1]);
         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 07:19 PM.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    However, if you had
    Code:
    char *stuff[2]={"Hello","World"};
    then you could swap the pointers via:
    Code:
    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, 01: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, 10:35 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21