Computing.Net > Forums > Programming > Adding 2 strings

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

Adding 2 strings

Reply to Message Icon

Name: SteveWalsh
Date: January 19, 2005 at 16:42:21 Pacific
OS: XP home
CPU/Ram: 2.01
Comment:

HI! i have a program where i need to add 2 strings to be more specific.

i want to do this
char string1[80] = '[' + string2 + ']';

language = C++. Thanks for any help.



Sponsored Link
Ads by Google

Response Number 1
Name: gimmpy225
Date: January 19, 2005 at 20:32:54 Pacific
Reply:

when you mean add 2 strings do you mean like

char str1[20] = 'HELLO WORLD';
char str2[20] = 'MY NAME IS JAMES';

and make it say HELLO WORLD MY NAME IS JAMES in str1 or 2? ( the [20] is just an example i know the sentence is longer :-P )

If yes, then use the concactante function.

I havnt worked in C++ in a few months, so ill go bust open my books and find out how to use the concactinate function.


GIMPS


0

Response Number 2
Name: Chi Happens
Date: January 20, 2005 at 05:27:47 Pacific
Reply:

Well it really all depends on what you have to use it for. Using Character Arrays as strings, means you need to use Character Array functions for concantenation:

char string2[10] = "wow";
char string1[80] = "[";
strcat(string1,string2);
strcat(string1,"]");
cout << string1 << endl; // outputs [wow]

Now, if you want to have serious ease of use and advanced features that are easy to implement (you can implement them with Character Arrays as well, but it requires you to reinvent these wheels), use the basic_string that c++ has:

string string_2 = "wow";
string string_1 = "[" + string_2 + "]";
cout << string_1 << endl; // outputs [wow]

the following program shows you how to use both of these:

-------------------
#include <string>
#include <iostream>

using namespace std;
-------------------

int main(int argc, char* argv[])
{
char string2[10] = "wow";
char string1[80] = "[";
strcat(string1,string2);
strcat(string1,"]");
cout << string1 << endl;

string string_2 = "wow";
string string_1 = "[" + string_2 + "]";
cout << string_1 << endl;

char dummy;
cin >> dummy;
return 0;
}
-------------------


Enjoy,
Chi


"They mostly come at night...mostly"


0

Response Number 3
Name: sen (by santanusen_82)
Date: January 26, 2005 at 22:27:11 Pacific
Reply:

Try overloading the + operator

Santanu Sen
National Institute of Technology
Durgapur
India


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


VB WinXP Controls Web Interface : Stocks



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Adding 2 strings

compare / interpret 2 strings C++ www.computing.net/answers/programming/compare-interpret-2-strings-c/11118.html

C++ comparing 2 strings www.computing.net/answers/programming/c-comparing-2-strings/11570.html

Adding tabs in a string in vb.net www.computing.net/answers/programming/adding-tabs-in-a-string-in-vbnet/11853.html