Thread: Easier way of concatenating a lot of strings?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    19

    Easier way of concatenating a lot of strings?

    I have a bunch of variable strings and was wondering if there was a quicker/easier way of sticking them all together into one big string other than with strcat(a, strcat(b, strcat(c,d))))

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    snprintf() is good:
    Code:
    char str[1024];
    snprintf(str, sizeof str, "%s%s%s%s", a, b, c, d);
    If you don't have snprintf(), you can use sprintf(); but be careful, because you can't tell sprintf() the size of your buffer. You also have to write into a new location; you can't "reuse" one of the strings, as in:
    Code:
    snprintf(a, sizeof a, "%s%s%s%s", a, b, c, d);
    Because this tries to write "a" into "a", it's not valid.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    19
    Cheers, that was just what I needed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the number of strings in a STRINGTABLE resource
    By eth0 in forum Windows Programming
    Replies: 1
    Last Post: 09-30-2005, 02:57 AM
  2. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  3. strings strings strings str..
    By Linette in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2002, 02:12 PM
  4. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 12:26 PM
  5. Sorting strings to speed up search?
    By Mox in forum C++ Programming
    Replies: 5
    Last Post: 09-10-2001, 12:17 PM