tags:

views:

914

answers:

5

Here is a simple count word program, which i feel is very efficient. Is this the best way to count words in C, or are there any flaws in this program?

         #include <stdio.h>
         int CountWords(void);
         main()
         {
            printf("count the words and enter string\n");
            CountWords();

         }
        int CountWords(void)
        {
            char c;
            int num=0;
            int flag= 0;
             while((c=getchar())!='\n')
            {
              if(c==' ')
              {
                flag=0 ;        
              }                         
              else if(flag==0)
              {
                num++;
                flag=1;     
              }                      
           }
              printf("Num is %d",num);
         }
+2  A: 

Your count will be off by one with this program. This is because you are checking for a space to add one to your count of words. What do you think will happen with the last word in the string?

AlbertoPL
A: 

Initialize flag=1 and the off by one error should go away.

pix0r
empty string and word count=1
KM
jst checked now but its not correct
+1  A: 

One thing I can think of is that it will count punctuation marks as words if they are surrounded by spaces.

E.g.

The quick - and lazy - fox.

will be reported as containing 7 words although there are only 5.
You may want to reduce the set of word characters to alphanumeric characters only, and counting punctuation marks as word separators - unless it is a ' or a - in the middle of a word, depending on what you define as a word (are it's and quick-witted single words ? Two words each ?).

RaphaelSP
A: 

Define your problem in terms of an algorithm first. For example, make a flow chart. Run some quick examples through your logic, just as other posters have done. Do all this before you write code.

Then, once you think you have the best algorithm, write that in C.

Come up with a list of questions to ask yourself, such as "What is a word?", "What is not a word?".

For parsing text, or tokens of any kind, you may be interested in expressing your ideas in terms of Backus-Naur Form. Take a look at any computer language specification, and notice how they define something like an identifier. Even if you don't use this to write your program, it should help you think out the problem.

Are words restricted to alphabetic characters a-z and A-Z? What about hyphenated words?

Perhaps (not in formal BNF):

hyphen := '-'
alpha := 'a' | 'b' | ... | 'z' | 'A' | 'B' | ... | 'Z'
alphagroup := alpha | alpha alphagroup
hyphenword := alphagroup hyphen alphagroup | alphagroup hyphen hyphenword
word := alphagroup | hyphenword

maxwellb
A: 

Ya But here is full code that can improve that program are works in all condition below is the code;

#include void CountWords(void); void main() { printf("\n\tcount the words and enter string\n\n\n"); CountWords();

     }
    void CountWords(void)
    {
        char c;
        int num=0;
        int flag= 0;
         while((c=getchar())!='\n')
        {
        if((c==' ')||(c=='  ')||(c=='.')||(c==';')||(c==',')||(c==';')
            ||(c==':')||(c=='"')||(c=='?')||(c=='!')||(c=='-'))
          {
            flag=0 ;        
          }                         
          else if(flag==0)
          {
            num++;
            flag=1;     
          }

       }
          printf("\t\n\n\nNumber of words is %d\n",num);
     }

/By Md. azaz/

Md. Azaz