Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

K and R exercise 1-22

Tags:

c

I am doing programs in The C Programming Language by Kernighan and Ritchie.

I am currently at exercise 1-22 that says:

Write a program to "fold" long input lines into two or more shorter lines after the last > non-blank character that occurs before the n-th column of input. Make sure your program does something intelligent with very long line, and if there are > no blanks or tabs before the specified column.

My problem is not how to do the program... My problem is where am I supposed to fold the line.

What does that "after the last non-blank character" means? Where should I fold the line?

Please guys DON'T tell me the program itself. I want to do it on my own.

I just want to know the point where I am supposed to fold the line.

EDIT: There is another problem...

What if my line contains tabs? In that case column length of the line increases from the character length. Which length should be considered?

EDIT 2:

Something intelligent... To tackle the case of tabs, I have an idea...

There are 2 exercise before 1-22 -- 1-20 and 1-21. Which are entab and detab... you guys must be knowing... So I will first detab the line then fold it and then entab the folded lines... I think that's gonna be the most intelligent in case of tabs. Any one?

like image 632
Sam Avatar asked Feb 10 '13 12:02

Sam


2 Answers

I think this exercise is basically emulating a "word-wrap" function you might see on a text editor. "blank" would mean "whitespace", e.g. space or tab characters.

Example:

"Once upon a time"

If you were asked to "fold" that line after the 14th column of input, the 14th character is the "i" in "time". Your output should be two lines: "Once upon a", "time".

The question also says to be intelligent with very long lines.

For example,

"Once upon a time Once upon a time Once upon a time"

If you were splitting this line at 14 characters, you should consider the fact that the line must be split into more than two lines.

There is also the case when a word could be very long, preventing you from splitting it:

"Deoxyribonucleicacid is DNA"

If you split that at 14 characters, you would not find any whitespace characters until " is", so the word should be split.

Finally, I'd agree that the question is slightly ambiguous, but as long as you're writing code somewhat along the lines of what is required, you're learning, so I wouldn't worry about getting too hung-up on the details. Good luck :)

like image 189
ChrisC Avatar answered Nov 15 '22 23:11

ChrisC


Consider this example: let's say n = 6 (for the simplicity of counting) and the line that you need to fold looks like this:

Got all chars on one line.
00000000001111111111222222
01234567890123456789012345

Go through the characters counting the current position in the line that you are producing. When you see a whitespace, go back to find the last non-space char before it, and makr its position. When your counter reaches n, see where was the last non-blank character preceding a space in the original line, and fold the line.

In the example above, the first space that you see is at the position 3. At this point you go back one char, mark 2 as the last non-blank preceding the space, and go on. When the count reaches 6 (at character 'l'), split the line at position 3, and reset the current position back to zero. Continue with the algorithm to fold at 7, 13, and 20.

Got
all
chars
on one
line
like image 38
Sergey Kalinichenko Avatar answered Nov 15 '22 22:11

Sergey Kalinichenko