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..
Printable View
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..
This is a C++ forum, don't use sprintf, use a stringstream.
There's one messy way...Code:char str1[] = "Hello ";
char str2[] = "world!";
char *str3 = new char[strlen(str1) + strlen(str2) + 1];
strcpy(str3, str1);
strcat(str3, str2);
And finally one using a stringstream.Code:stringstream ss;
ss << "Hello " << "world!";
cout << ss.str().c_str();
Then again there's the string class.
Don't be ridiculous. A function is a function. There is nothing wrong with using sprintf.Quote:
This is a C++ forum, don't use sprintf, use a stringstream
http://www.parashift.com/c++-faq-lit....html#faq-15.1Quote:
Originally posted by Sebastiani
Don't be ridiculous. A function is a function. There is nothing wrong with using sprintf.
Sure, we're only concatenating two strings... might as well use void main() {}; :rolleyes:
Ever play on any competitive sports teams? Every hear the expression you practice like you play? Same applies here.
what the heck is concatenation!?!?!:confused:
Adding. If I concatenate two strings, I add one to the other.Quote:
Originally posted by C "the smelly" gawd
what the heck is concatenation!?!?!:confused:
Quote:
Originally posted by Cgawd
what the heck is concatenation!?!?!:confused:
Code:char foo[] = "bling";
char bar[] = " turd";
strcat(foo, bar); // foo now is "bling turd"
is that c or c++?
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
The former.Quote:
Originally posted by Cgawd
is that c or c++?
ah...smart. Forgot bout that. :rolleyes:
Hmm, so much debate about such a vital and fundamental function..really is quite amusing...
Thanks ppl..
Eibro has a very good and valid point.
Use the superior and type-safe C++ functions and classes wherever possible.
This is getting interesting. Keep arguing for a bit longer.
I too am learning from each of your replies!