C语言题-输入一串字符,统计单词个数
代码:
#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\n", wordCount); getchar(); return 0; }
长风破浪会有时,直挂云帆济沧海!
可通过下方链接找到博主
https://www.cnblogs.com/judes/p/10875138.html