AlexGreat
AlexGreat

Reputation: 207

The C Programming K&R Exercise 1-11

I'm currently coming thru Word Counting program . And i got a tricky Exercise.
I can't understand what exactly this exercise want me to do .

Here the Exercise itself
Revise the word counting program to use better definition of "word," for example, a sequence of letters,digits and apostrophes that begins with a letter

I cant get what it actually want .It want me to count symbols digits apostrophes or ether it want me to put names for all those symbols digits etc. like "word," - will be A(") word comma(,) A(") or there something else.

This is the program which counts lines, characters, and new lines

#include <stdio.h>

#define YES 1
#define NO 0

main ()
{
    // CTRL+Z will Signal to EOF-End of File 
    int c,nl,nw,nc,inword;  //nl -new line
                            //nw -new word
                            //nc -new chatacter
                            //inword -program in word or not

    inword=NO;
    nl=nw=nc=0;
    while ((c=getchar()) !=EOF)
    {
        ++nc;
        if (c == '\n')
            ++nl;
        if (c == ' ' || c == '\t' || c == '\n')
            inword=NO;
        else if (inword==NO)
        {
            inword=YES;
            ++nw;
        }
    }
    printf("%d %d %d\n", nl,nw,nc);

    getchar(); 
}

Can you guys explain to me what actually the Exercise want me to do? I don't need already complete code. Iwant to go thru the coding myself. I just don't get what i actually need to do here in Exercise.

Upvotes: 4

Views: 2245

Answers (1)

Karoly Horvath
Karoly Horvath

Reputation: 96258

The program you posted treats everything which is delimited by whitespace as a word. So these are all words: 12 a,b123456 2-3@!#.

It asks you to improve the program with a better definition of word, and gives an example: "a sequence of letters,digits and apostrophes that begins with a letter".

So with this example John's and a123 is a word, but 12, 1a and a-b aren't.

Upvotes: 6

Related Questions