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 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.
Last edited by Eibro; 11-09-2002 at 09:14 PM.
Don't be ridiculous. A function is a function. There is nothing wrong with using sprintf.This is a C++ forum, don't use sprintf, use a stringstream
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; }
http://www.parashift.com/c++-faq-lit....html#faq-15.1Originally 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() {};
Ever play on any competitive sports teams? Every hear the expression you practice like you play? Same applies here.
Adding. If I concatenate two strings, I add one to the other.Originally posted by C "the smelly" gawd
what the heck is concatenation!?!?!
Originally posted by Cgawd
what the heck is concatenation!?!?!Code:char foo[] = "bling"; char bar[] = " turd"; strcat(foo, bar); // foo now is "bling turd"
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.Originally posted by Cgawd
is that c or c++?
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.
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
This is getting interesting. Keep arguing for a bit longer.
I too am learning from each of your replies!
Such is life.