如何统计一行字符串中有多少单词。
测出某个字符为空格则表示一个单词。如果一个字符前一个字符为空格则表示新单词开始
#include<stdio.h>
#define BUFFERSIZE 1024
int main()
{
char string [1024];
int i, count = 0, word = 0;
char c;
gets (string);
for (i=0; (c=string[i])!='\0'; i++)
{
if (c == ' ') //
word = 0;
else if(word == 0)
{
word = 1;
count++;
}
}
printf("一共有单词%d个\n",count);
return 0;
}
输入 Iam student
本文介绍了一种使用C语言统计给定字符串中单词数量的方法。通过遍历字符串并检查空格来确定单词边界,实现对单词计数的功能。
1792

被折叠的 条评论
为什么被折叠?



