如何在C中使用字符数组进行函数调用
这是一个显示字符串中字数的程序。 我设法进行了函数调用,但给出了一个错误“Arguments list syntax error”。 任何答案都会有很大的帮助如何在C中使用字符数组进行函数调用,c,arrays,string,count,character,C,Arrays,String,Count,Character,这是一个显示字符串中字数的程序。 我设法进行了函数调用,但给出了一个错误“Arguments list syntax error”。 任何答案都会有很大的帮助 #include<stdio.h> int wordCount(char str[],int b); main() { char str[100]; int b, d; clrscr(); // clear the screen every co
#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;
}
#包括
intwordcount(charstr[],intb);
main()
{
char-str[100];
int b,d;
CLRSC();//在每次编译和生成时清除屏幕
printf(“写下你的信息:”);
获取(str);//读取用户输入的str[]
b=strlen(str);//不使用
d=字数(str,b);
printf(“字数:%d”,d);
getch();
}
int count(str[],b)//错误指出的位置
{
int i=0,word=0;
对于(i=0;i
您必须在函数定义中指定参数的类型:
int count(str[],b){ ...
应该成为
int wordCount(char str[],int b){ ...
就像在函数声明中一样
另外,您必须指定main()
->int main()
另外,您必须
#包括才能使用strlen()
以下是我对您的代码所做的更改,现在可以使用了
#include<stdio.h>
int wordCount(char str[],int b);
main()
{
char str[100];
int b, d;
clrscr();
printf("Write your message: ");
gets(str);
b = strlen(str);
d = wordCount(str,b);
printf("No. of words: %d", d);
getch();
}
int wordCount(char str[],int b)
{
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;
}
#包括
intwordcount(charstr[],intb);
main()
{
char-str[100];
int b,d;
clrsc();
printf(“写下你的信息:”);
获取(str);
b=strlen(str);
d=字数(str,b);
printf(“字数:%d”,d);
getch();
}
整数字数(字符str[],整数b)
{
int i=0,word=0;
对于(i=0;i
在您的代码中,您没有按声明的那样键入正确的函数名,也没有给出参数的类型,这就是它给您带来错误的原因。您认为wordCount
函数实际上在哪里?不鼓励使用gets()
,因为它不安全。如果你有一个现代化的编译器,你应该得到一个警告。查看fgets()
另外,您需要包括
才能按预期方式使用strlen()
。它工作的唯一原因是@kennethsantianez,您可能应该用str[]==“abc”
,浏览一下,而(str[i]!=''&&str[i]!='\t'){i++;}
没有明确的理由停止循环。