How can I simplify the string concatenation?

Suppose I want concatenate four strings, I declare them as:
char a[100] = "aaa";
char b[100] = "bbb";
char c[100] = "ccc";
char d[100] = "ddd";

Instead of using strcat(), can I concatenate them to variable "a" using the following statement:
a = a+b+c+d;

is there anything i need to add/modify (such as adding some header or library files)?

Thanks!
qwe123qwe123Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pb_indiaCommented:
Yes. You can do so.

Add this header file:

#include <string.h>

Use this:
char a[100] = "aaa";
char b[100] = "bbb";
char c[100] = "ccc";
char d[100] = "ddd";


string str = string(a) + string(b) + string(c) + string(d);

You cannot directly add a+b+c+d without converting them to strings.
0
pb_indiaCommented:
//Also one more variation:

#include<stdio.h>
#include<stdlib.h>
#include <string.h>

string a = "aaa";
string b = "bbb";
string c = "ccc";
string d = "ddd";

//Now you can do the following
a=a+b+c+d


//Result: a=aaabbbcccddd
0
qwe123qwe123Author Commented:
I don't know why there are such errors as shown in the following.
The string variable can't be declared...
I have already include the header files #include <iostream.h>, #include <stdio.h>, #include <stdlib.h> and #include <string.h>. Is there anything I've missed?

error C2065: 'string' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'a'
error C2065: 'a' : undeclared identifier
error C2440: '=' : cannot convert from 'char [4]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2146: syntax error : missing ';' before identifier 'b'
error C2065: 'b' : undeclared identifier
 error C2440: '=' : cannot convert from 'char [4]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2146: syntax error : missing ';' before identifier 'c'
error C2065: 'c' : undeclared identifier
error C2440: '=' : cannot convert from 'char [4]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
 error C2146: syntax error : missing ';' before identifier 'd'
error C2065: 'd' : undeclared identifier
error C2440: '=' : cannot convert from 'char [4]' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
error C2146: syntax error : missing ';' before identifier 'cout'
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

qwe123qwe123Author Commented:
Just for your information, I am using Visual C++ 6.0
0
itsmeandnobodyelseCommented:
>> #include <string.h>

That's wrong. For STL string class you need that instead

#include <string>
using namespace std;

Regards, Alex


0
rstaveleyCommented:
#include <iostream> // <- no .h
#include <string>
using namespace std;

// ...etc.
0
itsmeandnobodyelseCommented:
>> I have already include the header files #include <iostream.h>, <stdio.h>, <stdlib.h>

Try to use only that

#include <iostream>
#include <string>
using namespace std;

Regards, Alex



0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rstaveleyCommented:
<string.h> is equivalent to <cstring> in the modern headers. It doesn't give you std::string, but it gives you a load of whistles and bells for null terminated character arrays (or C strings), like your friend strcat.
0
itsmeandnobodyelseCommented:
When using VC6 and STL headers, put the following pragma statement at the first line of all cpp files:

#pragma warning ( disable : 4786 )

That prevents a zillion of silly compiler warnings regarding Debug symbols from showing up at every build.

Regards, Alex


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.