+ 9
Solving some c++ codes
#include <iostream> #include <string> int main() { std::string str1 ("We just Joking"); std::cout << str1.substr(8); return 0; } Why the output is "Joking" and please give me some.. explanation about "str1.substr(8)" ╮(°_°)╭
4 ответов
+ 7
In C++, std::substr() is a predefined function used for string handling.
This function takes two values pos and len as an argument and returns a newly constructed string object with its value initialized to a copy of a sub-string of this object. Copying of string start from pos and done till pos+len means [pos, pos+len).
In this case, pos = 8 ,that is,
0 - W
1 - e
2 -
3 - j
4 - u
5 - s
6 - t
7 -
8- J
so the output is Joking.
Hope you understand
+ 9
Thank you for all of your help.. ^_^b
+ 8
The string data type acts like an array in that it has an index which starts with the leftmost character at zero. (You can print out any of the string characters like this: std::cout<<str1[ ]; -- put a number in the brackets.) Then we count to the right, along the main string until we get to index 8, which is "J". The dot-operator substr() will look for a smaller string within the larger original beginning at index 8 and continuing until the end of string or if you specify where to stop.
+ 4
simply the substring str1 strats from 8th character of the total string. In that string 8th character is 'J'.
hence str1 is JOKING
https://code.sololearn.com/cbm5ODAH21r9/?ref=app
when you use str1.substr(0);
output will be total string