1. 要求: 编写一个程序, 以每行一个单词的形式打印其输入.

2. 分析.

1. 在每个单词的结尾处换行, 单词结尾的判断, 即前一个字符即不是空格, 制表符, 也不是换行符,后面跟着空格或制表符或换行符.

2. 在遇到不是空格,不是制表符, 不是换行符时输出, 设置标记为1;

3. 遇到空格或制表符或换行符时, 同时判断标记

  1. 标记是1则打印换行符(\n), 重置标记位为0.
  2. 标记是0(表示上一个输入字符是空格或制表符或换行符)则不处理, 什么也不打印.

3. 代码

注意: 在执行的时候每次回车都是输入, 程序在while就开始循环判断, 同时打印数据. C程序设计语言练习1-12之前的习题则在EOF之后才打印.

#include<stdio.h>

#define IN 1
#define OUT 0

int main(){
    int c, state;
    state = OUT;
    while( (c= getchar() ) !=EOF ){
        if( c != ' ' && c != '\t' && c != '\n'){
            putchar(c);
            state = IN;
        }else if( ( c == ' ' || c == '\t' || c != '\n') && IN == state ){
            state = OUT;
            printf("\n");
        }else if( ( c == ' ' || c == '\t' || c != '\n') && OUT == state ){
            continue;
        }
    }
    return 0;
}

可简化写为.

#include<stdio.h>

#define IN 1
#define OUT 0

int main(){
    int c, state;
    state = OUT;
    while( (c= getchar() ) !=EOF ){
        if( c != ' ' && c != '\t' && c != '\n'){
            putchar(c);
            state = IN;
        }else if( IN == state ){
            state = OUT;
            printf("\n");
        }
    }
    return 0;
}