【发布时间】:2016-07-03 10:36:18
【问题描述】:
这是一个显示字符串中单词数量的程序。 我设法进行了函数调用,但给了我“参数列表语法错误”的错误。 任何答案都会有很大帮助。
#include<stdio.h>
int wordCount(char str[],int b);
main()
{
char str[100];
int b, d;
clrscr(); // clear the screen every compile and build
printf("Write your message: ");
gets(str); //reads the str[] which the user input
b = strlen(str); // run without the <string.h>
d = wordCount(str,b);
printf("No. of words: %d", d);
getch();
}
int count(str[],b) // Where the error points out
{
int i=0,word=0;
for (i = 0; i < b; i++)
{
if (str[i] != ' ' && str[i] != '\t')
{
word++;
while (str[i] != ' ' && str[i] != '\t')
{
i++;
}
}
}
return word;
}
【问题讨论】:
-
你在哪里认为
wordCount函数实际上是? -
不鼓励使用
gets(),因为它不安全。如果你有一个现代编译器,你应该得到一个警告。查看fgets()此外,您需要包含<string.h>才能以预期方式使用strlen()。它起作用的唯一原因是For C, all undeclared functions are implicitly declared to return int and take an unspecified number of unspecified arguments.
标签: c arrays string count character