Home c++ String Concatenation

String Concatenation

Author

Date

Category

I am writing a term paper on MFC. I use MySQL in it. What can be used for convenient string concatenation – you need to constantly from forms, insert into a char’s string (query) and transfer. I realized that PHP won’t work)


Answer 1, authority 100%


Answer 2, authority 30%

string s   = "Hello, ";
string s1  = "world!\n";
string sum = s + s1;

Answer 3, authority 10%

Functionstrcatfunction appends the string passed in the first parameter to the string passed in the second parameter. In this case, strings are arrays of char elements. In C++, arrays are not dynamic structures, that is, they cannot change their size during program execution. Therefore, the array (string) passed as the first parameter to strcatmust have enough space to accommodate both strings. By declaring a line like this

char* slovo1  =  " proger ";

you create an array of char's exactly as long as the string "proger" (plus one null-terminated element). If you want to concatenate two lines, you have to allocate memory for the result:

cahr* slovo = new char[strlen(slovo1) + strlen(slovo2) + 1];
strcat(slovo, slovo1);
strcat(slovo, slovo2);
printf(slovo);

There are other ways as well. For example using the stringstream class:

stringstream ss;
ss << slovo1 << slovo2;
printf(ss.str().c_str());  // so orcout << ss;
Or using the classstring:
string slovo1 = " proger ";
string slovo2 = " riger ";
slovo1 = slovo1 + slovo2;
cout << slovo1;

Answer 4

The class is best used naturally as string, since this is a standard C++ class. It has an overloaded + = operator that adds characters and an append method.

class string - more info

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions