0

String + int

I just tried this cout << 1 + "zohal"; And the out put is ohal!! And check this out if I add 2 the output will be hal Why?? If I have a string variable and do the same thing it either be a CE or a weird output I tried it Is there anyway to do the same thing with a string variable?

8th Jan 2020, 6:44 PM
Zohal
Zohal - avatar
2 Respostas
+ 1
I'm not a C++ programmer, but I believe it's used to get a substring starting from the index added. For example, if we have a string "Hello World!", and we want to get "World!", we can do: cout<< 6 + "Hello World!"; This removes the "Hello ".
8th Jan 2020, 7:46 PM
Jianmin Chen
Jianmin Chen - avatar
0
The same can be written as const char * a = "name"; std::cout << a + 2; Or std::cout << 2 + a; The last permutation give the same output becouse 'a' is memory address, so if a=3542, 3542+2=2+3542. and we need to remember that terminator of string literal is \0.
8th Jan 2020, 8:24 PM
Ss13
Ss13 - avatar