|
-
Feb 19th, 2003, 11:00 AM
#1
Thread Starter
yay gay
Junction 2 strings
how would u do this?
i have the following strings:
char *str1 = "123";
char *str2 = "456";
and now i want to print "123456"
what is the best way to do that?
\m/  \m/
-
Feb 19th, 2003, 03:56 PM
#2
Hyperactive Member
How about that:
Code:
printf("%s%s", str1, str2);
-
Feb 19th, 2003, 04:10 PM
#3
or you could use the standard string class
Code:
#include <string>
#include <iostream>
using std::string;
using std::cout;
// ....
string one = "part one";
string two = "and";
string three = "two";
one += ' ' + two + ' ' + three + '.';
cout << one;
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Feb 19th, 2003, 05:32 PM
#4
Thread Starter
yay gay
do ppl use string type instead of using char arrays? i want to know what the pro guys do lol :P
made_of_asp: well imagine i want to work out that char array..using printf wont work here then
anyways..there is here a thing that does confusion to me........how much spaces/slots in a char array u usually put? 30? 60? 200? the performance difference is big?
\m/  \m/
-
Feb 19th, 2003, 11:27 PM
#5
Hyperactive Member
Originally posted by sunburnt
or you could use the standard string class
Code:
#include <string>
#include <iostream>
using std::string;
using std::cout;
// ....
string one = "part one";
string two = "and";
string three = "two";
one += ' ' + two + ' ' + three + '.';
cout << one;
you can also do
Code:
sprintf(szString, "%s%s", string1, string2);
String class is very heavy and is part of STL. I mostly write C-style code, so i can't use it.
-
Feb 20th, 2003, 03:37 AM
#6
Thread Starter
yay gay
so i should avoid using the string class?
\m/  \m/
-
Feb 20th, 2003, 07:56 AM
#7
so i should avoid using the string class?
No, that's what it's for. The standard library's string class is not really slower then c strings, sometimes even faster. It is also much less error prone. By using string you don't have to worry about allocating and freeing memory, whether or not you need to append a '\0', etc. So, unless you either a. Must use plain C; or b. Are a wizard programmer writing kernel code, use std::string.
-
Feb 20th, 2003, 01:36 PM
#8
Thread Starter
yay gay
tks all
in which case its faster than c strings(char arrays)?
\m/  \m/
-
Feb 20th, 2003, 03:17 PM
#9
Thread Starter
yay gay
Originally posted by sunburnt
or you could use the standard string class
Code:
#include <string>
#include <iostream>
using std::string;
using std::cout;
// ....
string one = "part one";
string two = "and";
string three = "two";
one += ' ' + two + ' ' + three + '.';
cout << one;
that isnt workin on vc++7...what should i change?
\m/  \m/
-
Feb 20th, 2003, 03:53 PM
#10
Hyperactive Member
You shouldn't really avoid STL. But it does increase file size (greatly).
I can do everything in STL with plain C, so I don't really need it. I don't think string class is faster than char arrays, it has an overhead. But I might be wrong.
-
Feb 21st, 2003, 06:23 AM
#11
I don't think string is ever faster either. But it is written to cause very little runtime overhead.
And its ease of use makes up for any disadvantages.
I always use string except when I'm into absolute speed, and I seldom am.
that isnt workin on vc++7...what should i change?
Working for me if I put the actual code in main.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 21st, 2003, 09:01 AM
#12
Thread Starter
yay gay
hmmm ok then ill investigate about the string class
\m/  \m/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|