Thread: concatenation

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    concatenation

    This may sound very noobish, but how can I take one string x and another y and get a final string w without modifing either x or y. The strcat() function always edits its first parameter. Thanks..

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    This is a C++ forum, don't use sprintf, use a stringstream.

    Code:
    char str1[] = "Hello ";
    char str2[] = "world!";
    
    char *str3 = new char[strlen(str1) + strlen(str2) + 1];
    strcpy(str3, str1);
    strcat(str3, str2);
    There's one messy way...

    Code:
    stringstream ss;
    
    ss << "Hello " << "world!";
    cout << ss.str().c_str();
    And finally one using a stringstream.
    Then again there's the string class.
    Last edited by Eibro; 11-09-2002 at 09:14 PM.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    This is a C++ forum, don't use sprintf, use a stringstream
    Don't be ridiculous. A function is a function. There is nothing wrong with using sprintf.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Sebastiani
    Don't be ridiculous. A function is a function. There is nothing wrong with using sprintf.
    http://www.parashift.com/c++-faq-lit....html#faq-15.1

  5. #5
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Sure, we're only concatenating two strings... might as well use void main() {};

    Ever play on any competitive sports teams? Every hear the expression you practice like you play? Same applies here.

  6. #6
    what the heck is concatenation!?!?!

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by C "the smelly" gawd
    what the heck is concatenation!?!?!
    Adding. If I concatenate two strings, I add one to the other.

  8. #8
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Originally posted by Cgawd
    what the heck is concatenation!?!?!
    Code:
    char foo[] = "bling";
    char bar[] = " turd";
    strcat(foo, bar); // foo now is "bling turd"

  9. #9
    is that c or c++?

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Fine! Go push your C-Functions on the C Board, but for C++, stringstreams are a better choice.

    I'll give you some more reasons besides being "unable to keep track of your buffer sizes" to use C++ strings. I won't even stray from that website. http://www.parashift.com/c++-faq-lit....html#faq-33.1

  11. #11
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Cgawd
    is that c or c++?
    The former.

  12. #12
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    ah...smart. Forgot bout that.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Posts
    2

    Talking

    Hmm, so much debate about such a vital and fundamental function..really is quite amusing...

    Thanks ppl..

  14. #14
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Eibro has a very good and valid point.

    Use the superior and type-safe C++ functions and classes wherever possible.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  15. #15
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158
    This is getting interesting. Keep arguing for a bit longer.

    I too am learning from each of your replies!
    Such is life.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Concatenation Using Pointers No strcat()
    By oviang in forum C Programming
    Replies: 4
    Last Post: 12-07-2007, 10:31 AM
  2. printing arrays with concatenation
    By derek23 in forum C Programming
    Replies: 1
    Last Post: 07-17-2005, 03:02 AM
  3. concatenation
    By rose2626 in forum C++ Programming
    Replies: 10
    Last Post: 04-25-2003, 01:27 PM
  4. queue concatenation
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-02-2002, 06:35 AM
  5. integer concatenation with string again...
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-11-2002, 06:36 PM