int main(){ int c, n; n = 0; while((c =getchar())!=EOF){ if(c !=' '&& c !='\t'&& c !='\n') n ++; while(c !=' '&& c !='\t'&& c !='\n') c =getchar();//因为get到空格不算空格都可以,所以不ungetc了
} printf("%d\n", n); }
Version 2:
#include<stdio.h>
int main(){ int c, state, nw; state = 0;//outside
nw = 0; while((c =getchar())!=EOF){ if(c ==' '|| c =='\t'|| c =='\n') state = 0; elseif(state == 0){ nw++; state = 1; } } printf("%d\n", nw); }