Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

c++ - How many spaces for tab character( )?

I want to implement a text drawing function. But I am not sure how works, which means I don't know how many spaces I should print for .

I have come up with the following algorithm:

a) Each represents at most NUMBER_OF_SPACES_FOR_TAB spaces. b) If appears in the last line at a corresponding position, for this line should be aligned to the of last line.

Example:

printf("ab
");
printf("c
");

Should print:

a11112222b
34444c

Where:

1.Number i represents the spaces of at position i

2.NUMBER_OF_SPACES_FOR_TAB == 4

Does anyone know the standard algorithm? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A tab character should advance to the next tab stop. Historically tab stops were every 8th character, although smaller values are in common use today and most editors can be configured.

I would expect your output to look like the following:

123456789
a       b
        c

The algorithm is to start a column count at zero, then increment it for each character output. When you get to a tab, output n-(c%n) spaces where c is the column number (zero based) and n is the tab spacing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...