Combination of the string

Hi, need some help here. I am new with C++. For example, i have 2 string named A and B that is input by user. I want to combine those 2 string become 1 which is named C without any space. Can someone help me. Thank you.

1
2
3
4
  string A, B, C;
  cout << "Please input A and B";
  cin >> A;
  cin >> B;
to concatenate strings you can just add them with the + operator:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>

int main()
{
    std::string a{}, b{};
    getline(std::cin, a);
    getline(std::cin, b);
    std::string c = a + b;
    std::cout << c << "\n";
}

edit: and if you do decide to have some space b/w the 2 original strings you could do:
1
2
3
4
 std::string c = a + " " + b;
    //though purists might prefer using the std::string ctor explicity:
    //std::string c = a + std::string{" "} + b;
    //http://stackoverflow.com/questions/6061648/concatenate-two-string-literals 
Last edited on
Topic archived. No new replies allowed.