Replace the tabs in the input with appropriate spaces to fill the spaces with the ending position of the next tab.

Source: Internet
Author: User

 

# Include <stdio. h>

# Define tabinc 8 // define each tabinc (8) position with a tab

Int main ()
{
Int C, Nb, Pos;

NB = 0; // the number of spaces required to reach the next tab when a tab appears
Pos = 1 ;// Program Current position of the text line
While (C = getchar ())! = EOF)
{
If (C = '/t ')
{
NB = tabinc-(POS-1) % tabinc;
While (NB> 0)
{
Putchar ('');
++ Pos;
-- Nb;
}
}
Else if (C = '/N ')
{
Putchar (C );
Pos = 1;
}
Else
{
Putchar (C );
++ Pos;
}
}

Return 0;
}

1. Understanding the meaning of the question: the purpose of this question is to replace the input '/t' with a proper number of spaces for output. For example, enter:

AB/tcde/TAS

POs: 123456789, (POS is the position of the input character, a -- the first input character, c -- the fourth input character)

Output: AB cde as, (there are 6 spaces between B and C, and 5 spaces between E and)

 

Note: in fact, the string input on the terminal is the same as the output style. Here we write it as AB/tcde/TAS, mainly for the sake of intuition.

It looks nice. The/T in the above string is not displayed when the terminal inputs, because this type of escape character is invisible, the compiler

By default, several spaces are used instead of/t.

 

2. tabinc: A production table appears at every eight positions. The POS is the current position of the program in the text line, and the Nb is when a tab is displayed,

The program calculates the number of spaces required to reach the next tabulation.

 

3. read characters are divided into three types: '/t','/N', and others. Process them separately.

NB = tabinc-(POS-1) % tabinc;

While (NB> 0 ){

Putchar ('');

++ Pos;

--Nb;

Let's talk about this section.CodeIf you have used a tab, you will find that a tab occupies 8 characters in some editing software, and the position of the tab is an integer multiple of eight, for example, if you enter 123, the cursor is at the fourth position. When you enter a tab, the cursor will stay at the ninth position of the row, that is, the five spaces will pop out. If you enter 1234567890, the cursor is at the second position, and then the tab is entered. The cursor stays at the second position of the row, that is, six spaces are displayed. (Multiples of 8)

Tabinc-(POS-1) % tabinc;

Tabinc is the width of the tab, set to 8. POS is the position of the cursor in this line, (pos-1) is the position of the previous character. Find the remainder of the line with tabinc, and use tabinc-to obtain the number of characters that should be filled with spaces.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.