我使用的DEVC++编译器,新建get_words_num.c文件
#include<stdio.h>
int main()
{
char cString[100];
int iIndex,iWord=1;
char cBlank;
gets(cString);
if(cString[0]=='\0')
{
printf("There is no words!\n");
}
else if(cString[0]==' ')
{
printf("first words is a blank char!\n");
}
else
{
for (iIndex=0;cString[iIndex]!='\0';iIndex++)
{
cBlank=cString[iIndex];
if(cBlank==' '){
iWord++;
}
}
printf("%d\n",iWord);
}
return 0 ;
}
运行
I love cats and dags
5
I love cats and dags
first words is a blank char!
直接回车
There is no words!