Selected solutions to exercise of "The C Programming Language" 2e (Part 1)

Exercise 1-8

Write a program to count blanks, tabs, and newlines.

#include <stdio.h>

 

int main(void)

{

    int blanks, tabs, newlines;

    int c;

    int done = 0;

    int lastchar = 0;

   

    blanks = 0;

    tabs = 0;

    newlines = 0;

   

    while(done == 0) {

        c = getchar();

       

        if(c == ' ')

            ++blanks;

        else if(c == '/t')

            ++tabs;

        else if(c == '/n')

            ++newlines;

        else if(c == EOF) {

            if(lastchar != '/n') {

                /* this is a bit of a semantic stretch, but it copes

                * with implementations where a text file might not

                * end with a newline. */

                ++newlines;               

            }

            done = 1;

        }

        lastchar = c;

    }

   

    printf("Blanks: %d/nTabs: %d/nLines: %d/n", blanks, tabs, newlines);

    return 0;

}

 

Exercise 1-9

Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.

#include <stdio.h>

 

int main(void)

{

    int c;

    int inspace;

   

    inspace = 0;

    while((c = getchar()) != EOF)

    {

        if(c == ' ')

        {

            if(inspace == 0)

            {

                inspace = 1;

                putchar(c);

            }

        }

       

        /* We haven't met 'else' yet, so we have to be

         a little clumsy */

        if(c != ' ')

        {

            inspace = 0;

            putchar(c);

        }

    }

   

    return 0;

}

 

#include <stdio.h>

 

/* count lines in input */

int

main()

{

    int c, pc; /* c = character, pc = previous character */

   

    /* set pc to a value that wouldn't match any character, in case

       this program is ever modified to get rid of multiples of other

    characters */

   

    pc = EOF;

   

    while ((c = getchar()) != EOF) {

        if (c == ' ')

            if (pc != ' ')   /* or if (pc != c) */

                putchar(c);

            

            /* We haven't met 'else' yet, so we have to be

              a little clumsy */

            if (c != ' ')

                putchar(c);

            pc = c;

    }

   

    return 0;

}

 

#include <stdio.h>

 

int main(void)

{

    int c;

    while ((c = getchar()) != EOF) {

        if (c == ' ') {

            putchar(c);

            while((c = getchar()) == ' ' && c != EOF)

                ;

        }

        if (c == EOF)

        break; /* the break keyword is mentioned

               * in the introduction... */

       

        putchar(c);

    }

    return 0;

}

  
  
   
    
  
  

Exercise 1-10

Write a program to copy its input to its output, replacing each tab by /t , each backspace by /b , and each backslash by // . This makes tabs and backspaces visible in an unambiguous way.

/*

* Here's my attempt at a Category 0 version of 1-10.

*

* Gregory Pietsch

*/

 

#include <stdio.h>

 

int main()

{

    int c, d;

   

    while ( (c=getchar()) != EOF) {

        d = 0;

        if (c == '//') {

            putchar('//');

            putchar('//');

            d = 1;

        }

        if (c == '/t') {

            putchar('//');

            putchar('t');

            d = 1;

        }

        if (c == '/b') {

            putchar('//');

            putchar('b');

            d = 1;

        }

        if (d == 0)

            putchar(c);       

    }

    return 0;

}

 

/* another solution */

#include <stdio.h>

 

#define ESC_CHAR '//'

 

int main(void)

{

    int c;

   

    while((c = getchar()) != EOF)

    {

        switch(c)

        {

        case '/b':

        /* The OS on which I tested this (NT) intercepts

            * /b characters. */

            putchar(ESC_CHAR);

            putchar('b');

            break;

        case '/t':

            putchar(ESC_CHAR);

            putchar('t');

            break;

        case ESC_CHAR:

            putchar(ESC_CHAR);

            putchar(ESC_CHAR);

            break;

        default:

            putchar(c);

            break;

        }

    }

    return 0;

}

  
  
   
    
  
  

Exercise 1-11

How would you test the word count program? What kinds of input are most likely to uncover bugs if there are any?

0. input file contains zero words
1. input file contains 1 enormous word without any newlines
2. input file contains all white space without newlines
3. input file contains 66000 newlines
4. input file contains word/{huge sequence of whitespace of different kinds}/word
5. input file contains 66000 single letter words, 66 to the line
6. input file contains 66000 words without any newlines
7. input file is
/usr/dict contents (or equivalent)
8. input file is full collection of moby words
9. input file is binary (e.g. its own executable)
10. input file is
/dev/nul (or equivalent)

66000 is chosen to check for integral overflow on small integer machines.

#include <assert.h>

#include <stdio.h>

 

int main(void)

{

    FILE *f;

    unsigned long i;

    static char *ws = " /f/t/v";

    static char *al = "abcdefghijklmnopqrstuvwxyz";

    static char *i5 = "a b c d e f g h i j k l m "

        "n o p q r s t u v w x y z "

        "a b c d e f g h i j k l m "

        "n o p q r s t u v w x y z "

        "a b c d e f g h i j k l m "

        "n/n";

   

    /* Generate the following: */

    /* 0. input file contains zero words */

    f = fopen("test0", "w");

    assert(f != NULL);

    fclose(f);

   

    /* 1. input file contains 1 enormous word without any newlines */

    f = fopen("test1", "w");

    assert(f != NULL);

    for (i = 0; i < ((66000ul / 26) + 1); i++)

        fputs(al, f);

    fclose(f);

   

    /* 2. input file contains all white space without newlines */

    f = fopen("test2", "w");

    assert(f != NULL);

    for (i = 0; i < ((66000ul / 4) + 1); i++)

        fputs(ws, f);

    fclose(f);

   

    /* 3. input file contains 66000 newlines */

    f = fopen("test3", "w");

    assert(f != NULL);

    for (i = 0; i < 66000; i++)

        fputc('/n', f);

    fclose(f);

   

    /* 4. input file contains word/

    *    {huge sequence of whitespace of different kinds}

    *    /word

    */

    f = fopen("test4", "w");

    assert(f != NULL);

    fputs("word", f);

    for (i = 0; i < ((66000ul / 26) + 1); i++)

        fputs(ws, f);

    fputs("word", f);

    fclose(f);

   

    /* 5. input file contains 66000 single letter words,

    *    66 to the line

    */

    f = fopen("test5", "w");

    assert(f != NULL);

    for (i = 0; i < 1000; i++)

        fputs(i5, f);

    fclose(f);

   

    /* 6. input file contains 66000 words without any newlines */

    f = fopen("test6", "w");

    assert(f != NULL);

    for (i = 0; i < 66000; i++)

        fputs("word ", f);

    fclose(f);

   

    return 0;

}

 

Exercise 1-12

Write a program that prints its input one word per line.

#include <stdio.h>

 

int iswhitespace(int ch)

{

    if ((0x09 <= ch && ch <= 0x0D) || 0x20 == ch) {

        return 1;

    }

    else

        return 0;

}

 

int main(void)

{

    int c;

    int flag = 1;

    while((c = getchar()) != EOF) {

        if (iswhitespace(c) && flag) {

            flag = 0;

            putchar('/n');           

        }

        if (!iswhitespace(c)) {

            putchar(c);           

            if (!flag) {

                flag = 1;

            }

        }

    }

    return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值