Quote Originally Posted by bithub View Post
This is not a trivial problem for a beginner. You need to keep track of how many characters you are printing and only add spaces up to your tabstop column.
I agree, it's not for rank beginners, but it's not exactly an advanced problem either except in some special cases.

When outputting a character, all that is necessary is to track the displacement from the beginning of the line. When a tab is encountered, work out the separation between the next character and the next tab stop, and output that number of spaces.


Where it can be entertaining is that tab stops themselves are variable - some editors specify different intervals 2,3,4,5,6,8 etc, some editors allow the user to specify all tab stops, but information about the tab stops is not typically written to the actual file (instead being stored in some configuration file for the editor itself). That means the set of tab stops needs to be a program input (e.g. from a config file, command line options, and some sensible defaults if no such input is supplied).

It would be necessary to specify the behaviour required for any character input/encoding. What should happen if unprintable characters are encountered? How about handling vertical tabs? For a somewhat advanced topic, what about localisation (e.g. handling files for non-english speakers)?