The C Programming Language 1-9

 

The C Programming Language 1-9 — Windows Live

我用了位置标识的方法,虽然有点臃肿,不过道理还是很明显的

#include <stdio.h>

#define WAIT 0   //an initial place
#define FIRST 1  //a place in the first blank
#define OTHER 2  //a place in the balnk that follows
#define WORD 3   //a place in a word
#define FIRST_OVER 4

main()
{
    long state,c;
    state = WAIT;
    c = getchar();
    while (c != EOF)
    {
    //make state into different value
        if ((state == WAIT) && (c == ‘ ‘)){
                state = FIRST;
                }
        if ((state == FIRST_OVER) && (c == ‘ ‘)){
                state = OTHER;
                }
        if ((state == OTHER) && (c != ‘ ‘)){
                state = WORD;
                }
        if ((state == WAIT) && (c != ‘ ‘)){
                state = WORD;
                }
     //process the character string by different value of state          
        if (state == FIRST){
                putchar(c);
                state = FIRST_OVER;
                }
        if (state == WORD){
                putchar(c);
                state = WAIT;
                }
        c = getchar();
    }
    system("pause");
}

Leave a comment