Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is meant by trailing space and what's the difference between it and a blank?

Tags:

c++

c

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 ?

like image 321
user12448 Avatar asked Mar 08 '14 18:03

user12448


2 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.

like image 73
LeonardBlunderbuss Avatar answered Oct 08 '22 11:10

LeonardBlunderbuss


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)
like image 24
2785528 Avatar answered Oct 08 '22 11:10

2785528