What does Kernighan and Ritchie mean by 'trailing'?

Discussion in 'C Programming' started by Albert, Dec 30, 2005.

  1. Albert

    Albert Guest

    This isn't entirely related to C, but Kernighan and Ritchie asks in
    Execise 1-18 of their C programming language book to 'Write a program
    to remove trailing blanks and tabs from each line of input, and to
    delete entirely blank lines'. What do they mean by trailing?
    Albert, Dec 30, 2005
    #1
    1. Advertising

  2. Albert

    Guest

    Albert wrote:
    > This isn't entirely related to C, but Kernighan and Ritchie asks in
    > Execise 1-18 of their C programming language book to 'Write a program
    > to remove trailing blanks and tabs from each line of input, and to
    > delete entirely blank lines'. What do they mean by trailing?


    It means they trail the non-blank text.

    "what hath god wrought"

    has no trailing blanks, although it does have 3 internal blanks.

    "watson, come here I need you "

    has 4 trailng blanks after "you", the other block of 4 contiguous
    blanks are still internal. If you do the excercise correctly, you'll
    get

    "watson, come here I need you"

    You'll often find this implemented as a TRIM function which usually
    removes contiguous spaces from the start of a line also.
    , Dec 30, 2005
    #2
    1. Advertising

  3. Albert

    nelu Guest

    Albert wrote:
    > This isn't entirely related to C, but Kernighan and Ritchie asks in
    > Execise 1-18 of their C programming language book to 'Write a program
    > to remove trailing blanks and tabs from each line of input, and to
    > delete entirely blank lines'. What do they mean by trailing?


    It means 'following'.

    Let's consider <T> as a placeholder for a tab and <S> for a space. If
    you have the line:

    This<S>is<S>a<S><T>test.<S><S><T><S>

    Then the trailing spaces are the ones following the last printable
    character excluding the <S> and <T> characters themselves, i.e.
    <S><S><T><S> so your result would be:

    This<S>is<S>a<S><T>test.

    If you have a line like:

    <S><T><S><S>

    then all those characters should be removed and you're left with an
    empty string.

    I think this is called trimming. Some languages offer 3 functions:
    rtrim, ltrim and trim. rtrim trims the right (trailing) side of a
    string, ltrim trims the left (leading) side and trim both sides. Python
    has all three functions, although they're called rstrip, lstrip and
    strip and don't necessarily work only for spaces and tabs. I don't
    think C has any functions like that, although it should be easy to
    write down something simple. I guess that's what you have to do :)
    nelu, Dec 30, 2005
    #3
  4. On 29 Dec 2005 18:56:08 -0800, in comp.lang.c , "Albert"
    <> wrote:

    >This isn't entirely related to C, but Kernighan and Ritchie asks in
    >Execise 1-18 of their C programming language book to 'Write a program
    >to remove trailing blanks and tabs from each line of input, and to
    >delete entirely blank lines'. What do they mean by trailing?


    Trailing: lagging behind, coming after.
    Mark McIntyre
    --

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Dec 30, 2005
    #4
  5. Albert

    Mike Wahler Guest

    "Albert" <> wrote in message
    news:...
    > This isn't entirely related to C, but Kernighan and Ritchie asks in
    > Execise 1-18 of their C programming language book to 'Write a program
    > to remove trailing blanks and tabs from each line of input, and to
    > delete entirely blank lines'. What do they mean by trailing?


    const char *line1 = "This line contains two trailing blanks and one"
    " trailing tab character \t \n";

    const char *line2 = "This line contains no trailing spaces or
    " tab characters\n";


    const char *line3 = "\n"; /* this line is empty */
    -Mike
    Mike Wahler, Dec 30, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. niclane
    Replies:
    9
    Views:
    351
    André Brière
    Jun 20, 2005
  2. kaili
    Replies:
    1
    Views:
    350
    Simon Biber
    Jan 1, 2007
  3. Replies:
    13
    Views:
    4,247
    rideema
    Dec 17, 2008
  4. sandeep

    Questions about K&R (Kernighan and Ritchi)

    sandeep, Apr 22, 2010, in forum: C Programming
    Replies:
    57
    Views:
    1,180
    Tim Rentsch
    Apr 29, 2010
  5. fl
    Replies:
    0
    Views:
    282
Loading...

Share This Page