Thread: string concatenation

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    string concatenation

    How do i concantenate two strings in C++? Is there a built in function that does that? I want to take these two strings for example:

    Johnson

    Sam

    and make it one string as Johnson Sam

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    string concatenation

    How do i concantenate two strings in C++? Is there a built in function that does that? I want to take these two strings for example:

    Johnson

    Sam

    and make it one string as Johnson Sam

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    56

    string concatenation

    How do i concantenate two strings in C++? Is there a built in function that does that? I want to take these two strings for example:

    Johnson

    Sam

    and make it one string as Johnson Sam

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    sorry to make three posts, something strange happened when I sent the form...

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Code:
    std::string n1("Johnson");
    std::string n2("Sam");
    
    std::string nresult = n1 + std::string(" ") + n2;
    Donīt forget include correct headers.

    P.S. The moderators are gonna kill you for 3 identical threads
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    56
    i apologized in one of the three posts... i didn't mean to post three times, but the page wouldn't send for some reason... i was just too anxious.... anyway, thanks for the answer.

  7. #7
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Combining strings

    You can use strcat() to combine two strings together

    Code:
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    
    char stringarray[50];
    char string2[]="to be alive. ";
    
    strcpy(stringarray,"Isn't it a great day ");
    strcat(stringarray,string2);
    strcat(stringarray,"I think so.");
    
    cout<<stringarray<<endl;
    
    return 0;
    }
    OR You can use the STRING class which is much easier

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    string MyFirstname;
    string MyLastname;
    string FinalString;
    
    string Sentence="is my full name.";
    cout<<"Please Enter Your First Name:  ";
    cin>>MyFirstname;
    cout<<"Please Enter Your Last Name:  ";
    cin>>MyLastname;
    
    FinalString=MyFirstname+" "+MyLastname+" "+Sentence;
    
    cout<<FinalString<<endl;
    
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Uh.... strcat() or something like that. Should be easy to find in the function reference from the home page. Don't forget to concatenate a " " into that as well.

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Overloaded operators can be quite useful.
    Code:
    string str1="Foo", str2="Bar", str3;
    
    str3 = str1 + ' ' + str2; // str3 is now "Foo Bar"
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    I've always used strcat()... mostly because I don't know much about the string class yet...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    8
    When you know the string class, you'll never want to use strcat or any other C string functions.

  12. #12
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    >> When you know the string class, you'll never want to use strcat or any other C string functions.

    True. However, if you're working with older C code like the Windows API, you'll still have to learn what they are and how to use them.

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, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM