读《The C Programming Language》(3)

做了第一章的几个练习题。

Exercise 1-20. Write a program detab that replaces tabs in the input with the proper number of blanks to space to the next tab stop. Assume a fixed set of tab stops, say every n columns. Should n be a variable or a symbolic parameter?

#include <stdio.h>

#define TABWIDTH 8

main()
{
    int c, i;
    int nchar = 0; /* counts the char number before tab */

    while ((c = getchar()) != EOF)
    {
        if (c == '/t')
        {
            for (i = 0; i < TABWIDTH - nchar % TABWIDTH; i++)
                putchar(' ');
            nchar = 0;
        }
        else
        {
            putchar(c);

            if (c == '/n')
                nchar = 0;
            else
                nchar++;
        }
    }
}

Exercise 1-21. Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to achieve the same spacing. Use the same tab stops as for detab. When either a tab or a single blank would suffice to reach a tab stop, which should be given preference?

#include <stdio.h>

#define TABINC 8

main()
{
    int pos,       /* position indicator */
        counter,   /* counts the blanks in blank string */
        ntab,      /* tabs to be replaced with */
        nblank,    /* the rest blanks to fill */
        c;         /* receive the input char */

    pos = counter = 0;

    while ((c = getchar()) != EOF)
    {
        if (c == ' ')
        {
            pos++;
            counter++;
        }
        else
        {
            if (counter > 0) /* replace blanks here */
            {
                ntab = pos/TABINC - (pos-counter)/TABINC;
                if (ntab == 0)
                    nblank = counter;
                else
                    nblank = pos % TABINC;

                while (ntab > 0)
                {
                    putchar('/t');
                    ntab--;
                }
                while (nblank > 0)
                {
                    putchar(' ');
                    nblank--;
                }
            }

            if (c == '/t')
                pos += TABINC - pos % TABINC;
            else if (c == '/n')
                pos = 0;
            else
                pos++;

            counter = 0;
            putchar(c);
        }
    }
}

Exercise 1-22. 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 lines, and if there are no blanks or tabs before the specified column.

#include <stdio.h>

#define SW 80   /* screen width */
#define COL 40  /* column for folding line */

main()
{
    int c, pos, i;
    char input[SW];

    pos = 0;

    while ((c = getchar()) != EOF)
    {
        if (COL >= SW)
        {
            putchar(c);
            continue;
        }

        input[pos] = c;
        if (c == '/n')
        {
            input[++pos] = '/0';
            printf("%s", input);
            pos = 0;
        }
        else if (pos == COL)  /* exceed specified column */
        {
            while (pos >= 0)
            {
                if (input[pos] == ' ' || input[pos] == '/t')
                    break;
                pos--;
            }
            if (pos == -1 || pos == COL)
            {
                input[COL] = '/n';
                input[COL+1] = '/0';
                printf("%s", input);

                if (pos == -1)  /* no blank or tab */
                {
                    pos = 1;
                    input[pos] = c;
                }
                else  /* a blank or tab is at the end */
                    pos = 0;
            }
            else  /* wrap word here */
            {
                for (i = 0; i < pos; i++)
                    putchar(input[i]);
                putchar('/n');

                pos++;
                for (i = 0; pos <= COL; i++, pos++)
                    input[i] = input[pos];
                pos = i;
            }
        }
        else
            pos++;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值