-1

This is a program which display the number of words in a string. I manage to make a function call but gives me an error of "Arguments list syntax error". Any answer would be a such great help.

    #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;
    }
  • 1
    Where do you *think* the `wordCount` function actually *is* ? – WhozCraig Jul 03 '16 at 10:46
  • Using `gets()` is discouraged because it's not safe. If you have a modern compiler you should get a warning about that. Look at `fgets()` Also, you need to include `` to use `strlen()` in the intended way. The only reason it works is [For C, all undeclared functions are implicitly declared to return int and take an unspecified number of unspecified arguments.](http://stackoverflow.com/a/21327973/6180573) – smrdo_prdo Jul 03 '16 at 14:35

2 Answers2

4

You have to specify the type of your arguments in the function definition :

int count(str[],b){ ...

should become

int wordCount(char str[],int b){ ...

just like in your function declaration.

Plus, you have to specify the return type of main() -> int main()

Plus, you have to #include <string.h> to use strlen()

M. Timtow
  • 343
  • 1
  • 3
  • 11
  • @kennethsantianez You should probably cast a glance at http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list – M. Timtow Jul 03 '16 at 10:49
0

Here are the changes I made in your code, it will work now

#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;
}

In your code, you didn't typed the correct function name as you have declared and the type of parameter was not given either thats why it was giving you error.

Eddy
  • 344
  • 5
  • 16