What does Kernighan and Ritchie mean by 'trailing'?

A

Albert

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?
 
M

mensanator

Albert said:
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.
 
N

nelu

Albert said:
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 :)
 
M

Mark McIntyre

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
 
M

Mike Wahler

Albert said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top