Append certain number of characters to char[]?

This is a discussion on Append certain number of characters to char[]? within the C Programming forums, part of the General Programming Boards category; Code: char s1[] = "Lorem Ipsum"; char *s2 = s1; while (*s2 != 'e') ++s2; size_t len = 5; char ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    35

    Append certain number of characters to char[]?

    Code:
    char s1[] = "Lorem Ipsum";
    char *s2 = s1;
    while (*s2 != 'e') ++s2;
    size_t len = 5;
    char *s3;
    In the above code fragment, what would be the cleanest way to add len characters from s2 to s3?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    First, s3 doesn't point to writeable memory. Second, better example?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    First, you'd need to make s3 an array so it can hold any characters that you assign to it.

    You could use strncat with an =0 at the end. (strncpy if there are no other characters in the target string.)
    Code:
    strncat(s3, s2, len);
    s3[len] = 0;
    I think you want to pointers way, however:
    Code:
    while(len--) *s3++ = *s2++;
    *s3 = 0;
    That code modifies s3 and s2, of course. (You'd need to put the array elsewhere since you can't increment a pointer.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strncpy &| strncat with actual writable memory.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    35
    Quote Originally Posted by Dave_Sinkula
    First, s3 doesn't point to writeable memory. Second, better example?
    How about
    Code:
    char s1[] = "This is an example sentence.";
    char *start = s1;
    while (*start != ' ') ++start;
    char *end = start;
    while (*end != ' ') ++end;
    size_t len = end - start;
    char *s3 = malloc(len + 1);
    In this case, what would be the cleanest way to have s3 == "is" (preferably, including the '\0')?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    See my example and Quzah's cryptic reply; strncpy().

    Or you could use sprintf().
    Code:
    sprintf(s3, "%.*s", len, s2);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    35
    Code:
    strncpy(s3, s2, len);
    Code:
    sprintf(s3, "%.*s", len, s2);
    Code:
    while(len--) *s3++ = *s2++;
    *s3 = 0;
    Which is most preferable?

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Queue
    Code:
    strncpy(s3, s2, len);
    Code:
    sprintf(s3, "%.*s", len, s2);
    Code:
    while(len--) *s3++ = *s2++;
    *s3 = 0;
    Which is most preferable?
    Whichever you choose of the last two -- there's a bug in the first.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, strncpy() [edit] (+ the =0 as Dave has pointed out) [/edit] would probably be faster than sprintf() because that's its job; printf() would have to parse "%.*s" to figure out what to do. And the second may be slightly faster (or it may not, depending on optimisations); however, it changes s3, which is probably a bad idea, since that's your pointer to the malloc()'d memory.

    As an alternative, try this or a variation:
    Code:
    s3[len] = 0;
    while(len--) s3[len] = s2[len];
    Last edited by dwks; 09-16-2006 at 08:28 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Or even memcpy plus null termination.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    There are hundreds of ways to do it, take your pick. They pretty much boil down to a library function (such as strncpy, sprintf, memcpy, memmove, etc) or a loop of some kind.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    35
    Quote Originally Posted by dwks
    There are hundreds of ways to do it, take your pick. They pretty much boil down to a library function (such as strncpy, sprintf, memcpy, memmove, etc) or a loop of some kind.
    So it's basically a style/personal choice? Thanks everyone for all the input.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yes, pretty much. I usually use a function, possibly one that I wrote myself, because it's easier to read.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Queue
    So it's basically a style/personal choice? Thanks everyone for all the input.
    I like minimalistic standard functions, because they may be implemented in hand-optimized assembly to do things better than I know, or they might do the same as a simple function I might write -- generally as-good or better.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I guess memcpy() would be the fastest (in theory) because it doesn't need to look for a NULL as strncpy does, it just has to copy the right number of bytes. strncpy() might be better if the string to be copied might contain NULLs.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!!!!emergency Problem~expert please help
    By unknowppl in forum C++ Programming
    Replies: 9
    Last Post: 08-21-2008, 06:41 PM
  2. Read the number of characters
    By rehan in forum C++ Programming
    Replies: 5
    Last Post: 06-27-2007, 10:57 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  5. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM

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