The linked possible-duplicate question focuses on doing the arithmetic correctly. I'm guessing your problem is more primitive than that: you don't really know what a tab stop is.
When K&R wrote their exercises, they could expect the reader to have experience with typewriters, which is where tab stops come from. The tab key caused the carriage to slide over to the position of the next stop, which was a real physical thing that literally stopped the carriage from moving further until another key was pressed. (Later typewriters, becoming more computer-like, had programmed stop locations instead of physical stops.)
The purpose of tabs was tabulation (making tables). The tab stops were set at the horizontal locations separating the columns of the table, and then the table was entered by pressing the tab key after each value. For example, if I want to type this table:
Character ASCII
Tab 9
Linefeed 10
Carriage Return 13
Space 32
Without using tabs, I have to type space many times after the word "Tab", not as many times after "Linefeed", and only a few times after "Carriage Return". But if I set a tab stop at the position where the second column starts, it becomes easier. I press the tab key once after the word "Tab", and the carriage advances to the correct place for the 9, press the tab key once after the word "Linefeed", and it advances to the correct place for the 10, etc. If I needed a third column, I would set another tab stop a little bit to the right of the second column.
You can experience this in a text editor - not a fancy IDE that assigns all kinds of unrelated functions to the tab key, but a plain one like vi. Or even a terminal emulator, running a program that does nothing (cat > /dev/null
). Type several lines of words of various lengths, with a tab after each one, and observe how they line up, and what happens when one of your words is long enough to occupy 2 table columns. Keep playing with it until you have an intuitive understanding of what the tab character does.
Modern text editors and terminal emulators usually have tab stops set every 8 characters. That's what "tabstop = 8" means. The stops are at columns 8,16,24,32,..., or if you believe the leftmost column is 1 instead of 0, columns 9,17,25,33,... Tab stops are actually programmable on vt100-ish terminals, but that's a rarely used feature.
Back in your text editor, a the start of a new line, type 1 2 Tab 3 4. You get 12
and 34
separated by some whitespace made of a tab character. Then start another new line and type the same thing with a space before the tab: 1 2 space Tab 3 4. The second line looks exactly like the first, but this time the whitespace between 12
and 34
is made of a space and a tab. Make a third line that looks the same without using any tabs, by typing space until it lines up.
Those 3 lines are examples of possible input to the exercise 1-21 program. The first one, with a single tab and no spaces, uses the minimum number of characters so that's what you want to output for all 3 inputs.
If you need help figuring out a general formula for how many tabs and spaces to output, see the other question. Here's a rough description that leaves some details for you to think about: As you read the input, keep track of what column you're in. When you get to a space or tab, read to the end of the sequence of spaces and tabs, remembering what column you were in when the sequence started. At the end of the sequence, you know what column the cursor is in, and what column you want to move it to, and you must calculate the optimal sequence of spaces and tabs to make that movement.