--- In [email protected], "John Matthews" <[EMAIL PROTECTED]> wrote:
>
> --- In [email protected], Mickey Mathieson <user14312@> wrote:
> > The problem is the scanf implimentation. When the scanf encounters the
> > space character it signals end of string. So the only character read
> is 'd'. 
> 
> ...so you could use fgets():
> 
>     fgets(w, sizeof w, stdin);
> 
> http://www.opengroup.org/onlinepubs/009695399/functions/fgets.html

Also, if the entered string is length N, then the code will calculate
strlen(w) N+1 times, which is bad design. It would be better to use
something like:

    char *c;

    for (c = w; *c; c++)
    {
        if (*c == ' ')
            ...

The *c test in the 'for' loop tests whether the character pointed to
by c is non-zero. Zero is used to mark the end of C strings, so this
tests that the end of the string hasn't been reached.

BTW note that fgets() adds a newline character to the end of the
string which is read in, although it doesn't really affect this
program. Also, main should be defined as

    int main(void)

and it should return a value (0).

John

Reply via email to