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
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
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
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
sorry to make three posts, something strange happened when I sent the form...
Donīt forget include correct headers.Code:std::string n1("Johnson"); std::string n2("Sam"); std::string nresult = n1 + std::string(" ") + n2;
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!!!
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.
You can use strcat() to combine two strings together
OR You can use the STRING class which is much easierCode:#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; }
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; }
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.
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.
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
When you know the string class, you'll never want to use strcat or any other C string functions.
>> 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.