代码:
#include <stdio.h> #include <string.h> #define bool char #define N 1024 #define W 64 bool isWord(char word[], int length) { bool ret = 1; int i; for(i=0; i<length; ++i){ if((word[i]>='a' && word[i]<='z') || (word[i]>='A' && word[i]<='Z')){ }else{ ret = 0; break; } } return ret; } int main(int argc, char* argv[]) { int wordCount = 0; char info[N]; memset(info, 0, N); gets(info); int i; int length = strlen(info); char word[W]; memset(word, 0, W); int lastSpace = 0; for(i=0; i<length; ++i){ if(' ' == info[i]){ memcpy(word, info+lastSpace, i-lastSpace); if(1 == isWord(word, strlen(word))){ wordCount += 1; } lastSpace = i+1; memset(word, 0, W); } } printf("%d ", wordCount); getchar(); return 0; }