// 统计单词个数.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
int word(char ch[])
{
int i=0;
int j=0;
int num=0;
int len=0;
len=strlen(ch);
printf("%s\n",ch);
while ((i<len)&&(ch[i]!='\0'))
{
if (ch[i]==' ')
{
i++;
}
else
{
j=i-1;
//if (ch[j]==' ')
if (j<0||ch[j]==' ')
{
num++;
}
i++;
}
}
return num;
}
int main(int argc, char* argv[])
{
char ch[10];
gets(ch);
int num;
num=word(ch);
printf("%d\n", num);
return 0;
}
统计字符串中单词的个数
最新推荐文章于 2024-03-30 20:18:55 发布