• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2114
  • Last Modified:

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!
0
qwe123qwe123
Asked:
qwe123qwe123
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now