string to char to string

Why doesn't it print "BCA" ??
How to put "BCA" in b?
Thanks

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

std::string a,b;

int main()
{
   a= "ABC";
   b= a[1] + a[2] + a[0];

   std::cout <<b;
}
You are using the + operator on chars which will do simple integer addition.

You could do something like this instead:
 
b = {a[1], a[2], a[0]};

This will construct a new string containing the three characters a[1], a[2] and a[0] and then assign it to b.
Last edited on
thanks it works!
Topic archived. No new replies allowed.