看题主原代码,是想输出字符串中的英文单词个数?
说实话,题主的原代码没看懂。。。
先给题主一份【统计单词个数】的代码吧
如果我理解有误,题主再追问~~
#include <stdio.h>
#include <conio.h>
#define LEN 255 /*数组长度上限*/
/*判断字符是否为字母*/
int chIsLetter (char *ch) {
return ( (*ch>='a'&&*ch<='z') || (*ch>='A'&&*ch<='Z') );
}
/*统计字符串单词个数*/
int StrWordsCnt (char *str) {
int wordsCnt=0;
while (*str) {
if (chIsLetter(str)&&!chIsLetter(str+1)) /*当前字符为字母且下一字符不是字母*/
wordsCnt++; /*单词数+1*/
str++;
}
return wordsCnt;
}
int main (void) {
char str[LEN];
//char str[LEN] = " Life is a journey, not a destination.";
//char str[LEN] = "!HELLO WORLD.";
int wordsCnt=0;
gets (str);
//puts (str);
wordsCnt = StrWordsCnt (str);
printf ("%d\n", wordsCnt);
putchar ('\n');
getch (); /*屏幕暂留*/
return 0;
}
运行结果
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/267f9e2f070828381d88e5e4b399a9014d08f1bc?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)
注:上图运行结果略去输入字符串部分