Function TrimRight takes a line and removes all spaces at the end.
void TrimRight(char *s) // input "somestring " (3 spaces at the end)
{
// Here s == "somestring \0" and strlen(s) == 14
int last = strlen(s) - 2;
std::cout << "Last1: " << s[last] << std::endl; // Last == ' '
while (s[last] == ' ')
{
--last;
}
std::cout << "Last2: " << s[last] << std::endl; // Last == 'g'
s[last + 1] = '\0';
// Here s == "somestring" and strlen(s) == 10
}
Questions is why s!= "somestring/0" after TrimRight(s)? I'm using MVS 2017. Thanks.
You thought after TrimRight(s)
, s become something\0
.
But in TrimRight(s)
function, while loop
just passed from last index.
Pass mean it didn't delete whitespace
and \0
.
so s is not something\0
. It is something\0 \0
because of "just pass".