1. 程式人生 > >c語言實現wc功能

c語言實現wc功能

def () != int http .html unix har c語言

本隨筆對網站http://blog.chinaunix.net/uid-22566367-id-381958.html有所借鑒

#include <stdio.h>


#define BEGIN 1;

int main(int argc, char *argv[])
{
int characters, lines, words, state;
char c;

state = characters = lines = words = 0;
while((c = getchar()) != ‘0‘) {
characters++;
if(c == ‘\n‘) {
lines++;
state = 0;
continue;
} else if(c == ‘ ‘) {
state = 0;
continue;
} else if(c == ‘\t‘) {
state = 0;
continue;
} else {
if(state == 0) {
state = BEGIN;
words++;
}
continue;
}
}

printf("%d characters. %d words. %d lines.\n", characters, words, lines);
}

c語言實現wc功能