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;
}

 

posted @ 2021-07-17 00:15  朱小勇  阅读(833)  评论(0编辑  收藏  举报