Results 1 to 7 of 7

Thread: Concatenation

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134

    Concatenation

    I know in VB I would use '&' to combine strings, but how exactly would I do it in C++?

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    AFAIK, the plus (+) operator does it nicely in C, C++, and VB.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    If you are using strings from the string class, then you can just + them together.
    VB Code:
    1. #include <iostream>
    2. #include <string>
    3.  
    4. using namespace std;
    5.  
    6.  
    7. int main()
    8. {
    9.     string s = "Hello ";
    10.     string t = "there!";
    11.  
    12.     s += t;
    13.  
    14.     cout << s << endl;
    15.  
    16.    
    17.     return 0;
    18.    
    19. }

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Bah! I said that! Now I get to post a useless post as well! Ha!
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Yeah, the only difference is that you probably know what you are talking about when you answer questions, where as I am just a good guesser.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Originally posted by Sastraxi
    AFAIK, the plus (+) operator does it nicely in C, C++, and VB.
    Not in C it doesn't

    C doesn't have strings, you have NULL-terminated blocks of memory:
    Code:
    char first[100] = "Hello";
    const char *second = " World!";
    
    strcat(first, second);
    Of course, there are far more effective ways of doing this, but it would require messing around with malloc(), etc.

    Actually, there's nothing to stop you making your own string-type structure and some associated functions:
    Code:
    struct string_t {
        char *ptr;
        size_t allocated;
        size_t used;
    };
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Ontario, Canada
    Posts
    134
    " += " Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width