c++ - how to terminate a string using null charector -
I have created a new string using the existing string. Assume that the initial string is
string a = "sample";
and newly created string
string b = a;
Now I have put an empty character in the second index.
b [2] = '\ 0';
When I try to exclude B string
The output is shown as saple
.
I want to end the string after index -1.
Is this behavior normal? If this is normal, then how to eliminate the string after the first index.
Thank you ..
operator & lt; & Lt;
is too overloaded if you do so this behavior will be different:
four A [] = "sample"; Four b [size (a)]; Strakpy (b, a); B [2] = '\ 0'; Std :: cout & lt; & Lt; B;
or std :: cout & lt; & Lt; B.c_str ();
(which calls surcharge instead of std :: string
) The difference between the two overloaders is that how many surcharges for const char *
calls On the other hand, the use of the overload str.size ()
for std :: string
is set to print the characters (that is, the redundant null character) Does. The size of std :: string
includes any blank characters embedded in it. 1
To "sort" a string, you can follow and use:
std :: string a = " Sample "; Std :: string b = a; B.resize (2); Std :: cout & lt; & Lt; B;
1
Comments
Post a Comment