#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int isCharacter(char ch);
int main()
{
//方便测试加上 while(true)测试
while (1) {
int size = 1000;
char *word = (char *)malloc(size);
printf("请输入一串字符:");
gets(word);
int len = strlen(word);
int counts = 0;
int ptr = 0;
for (int i = 0; i < len; i++) {
ptr = i;
if (isCharacter(*(word+i))) {
while ((ptr < len) && isCharacter(*(word+ptr))) {
ptr++;
}
if (*(word + ptr) == ' ' || *(word + ptr) == '\0') {
counts++;
}
} else {
while ((ptr < len) && *(word + ptr) != ' ') {
ptr++;
}
}
i = ptr;
}
printf("单词个数为: %d\n",counts);
}
return 0;
}
int isCharacter(char ch) {
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
return 1;
}
return 0;
}
C语言统计单词个数
于 2022-01-21 11:30:08 首次发布