String concatenation

This is a discussion on String concatenation within the C++ Programming forums, part of the General Programming Boards category; Whenever I deem possible, and after completing a given exercise from the two books I'm using to learn C++, I ...

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435

    String concatenation

    Whenever I deem possible, and after completing a given exercise from the two books I'm using to learn C++, I try to do the same thing but this time with pointers, arrays, c-style strings, and lots, lots, of consts.

    I do this to try to consolidate my knowledge of these err... illusive constructs. However, I get this pervading feeling I could do it in a simpler way while still using the C style code. Maybe I can blame my Visual Basic background, and it's overly high-level nature, for this...

    This is one of those moments. The following function is my solution to one of the exercises found on C++ Primer 4th Edition. Basically, a minimalist solution to output the plural of a word.

    I would like you to comment on two things, if possible:

    1. Did I complicate things unecessarily by using strncpy() and strncat()? Is there a simpler solution?

    2. The dnamic c-string was the only way I found to be able to produce an array that can obviously only know its size at run-time. Had I choose not to create it dynamically, but instead fixed, would there be another solution other than passing the size of both strings as arguments to the function?

    Code:
    const char* plural(const char* word, const char* pls = "s") {
        char* ret = new char[strlen(word) + strlen(pls) + 1];
        strncpy(ret, word, strlen(word) + 1);
        strncat(ret, pls, strlen(pls) + 1);
        return ret;
    }
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'new' is C++, not C. Look up functions such as malloc and free. Don't cast them, because C doesn't need it. That's another C++ thing.

    Default arguments are also C++. If you're really trying to learn C++, then post in the appropriate forum. If you're posting C, post here, but not in C++.

    C99 supports arrays whose length is determined at runtime. Most compilers are not fully C99 compliant however, but will offer such extensions.


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

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    If some moderator sees this, could you please move to the C++ forum?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Correct.

    strcpy()
    strcat()

    Kuphryn

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,435
    Wouldn't strcat() and strcpy() still force me to pass the string dimensions to the function? Or are you refering to question 1?

    EDIT: (And aren't they unsafer solutions?)
    Last edited by Mario F.; 06-08-2006 at 06:55 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,656
    My solution here is to use snprintf(), but you will have to #include <cstdio>.
    PHP Code:
    int length snprintf(NULL0"%s%s"wordpls); 
    // snprintf() returns how many items it would have put in the string if it were big enough, 
    // so we call it with NULL and 0 first.
    char ret = new char[length];
    snprintf(retlength"%s%s"wordpls); 
    That looks about right. It's one of the safest solutions because there is always enough space for both strings in the destination buffer and it is guaranteed to be null terminated. This is not the case with string functions sometimes, if you use them badly.
    Last edited by whiteflags; 06-08-2006 at 07:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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