我是C编程新手,尝试编写一个代码来计算字符串中的字数。这里是我的代码,用于计算代码数。C:计算字数(需要帮助修复)
#include<stdio.h>
#include<string.h>
void main()
{
int count=0,i,len;
char str[100];
printf("enter the sentence");
gets(str);
len=strlen(str);
for(i=0;i<=len;i++)
{
if(str[i]==' ')
count++;
}
printf("the number of words are :\t%d",count+1);
}
当我的输入是:Here is four words
它工作正常。它给输出 the number of words are : 4
我的问题是我该如何处理“两个连续的空格”二字之间,输入和输入“在最后空间” “开头空间” 。
注意:请不要**使用'gets()'。这是一个安全漏洞,它已经从语言中被弃用,并在下一个标准中被删除。 – WhozCraig 2013-04-20 02:58:39
@WhozCraig我没有意识到这一点;我一直用它来简化它。你能指出我的替代品吗? – 2013-04-20 03:15:39
@WhozCraig我也一直使用gets()。你能指出我使用它的实际缺陷吗? – 2013-04-20 03:32:26