What is meant by trailing space and what's the difference between it and a blank ? I saw an exercise where there is a note about trailing space.
I didn't see the problem on that because cin
can ignore those spaces and catch only numbers ?
Trailing space is all whitespace located at the end of a line, without any other characters following it. This includes spaces (what you called a blank) as well as tabs \t
, carriage returns \r
, etc. There are 25 unicode characters that are considered whitespace, which are listed on Wikipedia.
what's the difference between [trailing space] and a blank?
A blank at the end of a line is a trailing space. A blank between characters (or words, or digits) is not a trailing space.
what is meant by trailing space?
Trailing space became a challenge for me for something I was trying to code. The challenge inspired me to create the following utility routines. For this particular effort, I defined "trailing space" as any "white space" at the end of a line. (Yes, I also created versions of this function for leading white space, and extra white space (more than 1 white space character in the middle of the line.)
const char* DTB::whitespaces = "\t\n\v\f\r ";
// 0 1 2 3 4 5
// 0)tab, 1)newline, 2)vertical tab, 3)formfeed, 4)return, 5)space,
void DTB::trimTrailingWhiteSpace(std::string& s)
{
do // poor man's try block
{
if(0 == s.size()) break; // nothing to trim, not an error or warning
// search from end of s until no char of 'whitespaces' is found
size_t found = s.find_last_not_of(DTB::whitespaces);
if(std::string::npos == found) // none found, so s is all white space
{
s.erase(); // erase the 'whitespace' chars, make length 0
break;
}
// found index to last not-whitespace-char
size_t trailingWhitespacesStart = found + 1; // point to first of trailing whitespace chars
if(trailingWhitespacesStart < s.size()) // s has some trailing white space
{
s.erase(trailingWhitespacesStart); // thru end of s
break;
}
}while(0);
} // void trimTrailingWhiteSpace(std::string& s)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With