what is meant by trailing space and what's the difference between it and a blank?
Olivia Zamora
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 ?
3 Answers
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) A trailing space in programming (as I think you're referring) it's a series of whitespaces at the end of a string or a line.
They can cause some hassle under the following circumstances:
You might have a string literal spanning multiple lines, in that case it can be tricky to debug a trailing whitespace
Can slow the development process when you always have to "fix" those by hand when you have to type-append on a line
Some parsing tools might have problems with them