Thread: concatenation of strings

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    90

    concatenation of strings

    how we can concatenate more then two strings .
    without using multiple strcat statement.
    example
    i have
    char *a="program";
    char *b="lang";
    char *c= "clang";
    so how can i add these strings without using two strcat.
    if i use more then one strcat .
    my execution times increases so how can i reduce it
    thank u
    sree

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    you cannot reduce execution time. You can however - write it with 1 line using sprintf

    sprintf(resbuf, "%s%s%s",a,b,c);

    Here there is a possibility of memory overrun, so I always use some snprintf equivalent
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's a bit of a laugh if you believe that somehow, despite the fact that you don't know how to code a string concatenate function, that you're going to (with a bit of help), write up something much more efficient than the standard library function.



    If your execution time is increasing, perhaps your function could be in-lined, but that's up to your compiler to accept or reject.

    If you'll step through your code, dollars to donuts, your code is wasting time mis-using the strcat function. When you step through your code, you'll see it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Concatenation of strings as char pointers
    By sameerc in forum C Programming
    Replies: 11
    Last Post: 05-10-2005, 04:23 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM