Thread: Concatinating Characters

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    71

    Concatinating Characters

    Hey guys, I am trying to take individual characters on an array and put them together into words on a new array. so if i have the word |n|i|c|e| in one array i want the other array to store the first element as |nice|. any ideas on how i can go about doing this?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    83
    do a search first because i've been through this before and this has been covered so many times.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    1D -> 2D (go figure!)

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    i did do a search before. I didnt find anything that put characters together into words on another array

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    The only difference between |n|i|c|e| and |n|i|c|e|\0| is the \0 at the end. char arrays == strings in C. If you have already figured out how to put these into an array, all you need to know is how to put \0 in there as well.
    hint:
    Code:
    array[last] = '\0';

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    if i put |n|i|c|e|\0| into a second array wouldnt it take up 5 elements in the array? this is an example of what i want it to look like:

    char[0] = n;
    char[1] = i;
    char[2] = c;
    char[3] = e;

    word[0] = nice;

    there will be more words in the array too.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So you need a 2D array of chars then.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    ok thanx for the help! i figured id have to do that or create an actual string. i thought there might be a simpler way. thanks again though

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Or use an array of pointers to char. This works better for variable length strings instead of being restricted by the second array index.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Whats you need is this

    Code:
        char chars[] = "nice";
        char *word[10] = { NULL };
        word[0] = chars;
        printf("%s", word[0]);
    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    71
    im reading in the input from a file one character at a time nd putting it on a table. i was going to find all the spaces between the and put the characters found up until that white space will be a word(or sequence of numbers) on another array but i think the 2D array will have to do

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM