-4

In this program a word is defined by any sequence of characters that does not include a "-", " ", "\n", ":" or "\t".

So "howdy-hi:slig" would be three words in this program.

while ((iochar = getchar()) != EOF) {

    if (iochar == '\n') {
        line++;
        word++;
    }

    if (iochar == ':' || iochar == '-' || iochar == '\t' || iochar == ' ') {
        word++;
    } 

Instead of incrementing word everytime a whitespace is encountered, I know I need to skip all the extra whitespace characters that can separate two words, so that "hello - my : name is Earl." counts 5 words instead of 8.

Thank you, I appreciate it.

Basti
  • 373
  • 2
  • 6
  • 12
collinskewl2
  • 75
  • 1
  • 3
  • 2
    Basic programming. Not an SO question. What have you tried? – John3136 Feb 20 '15 at 02:24
  • Adds little value to SO. The problem should be short enough to post in its entirety, so why not post what you have so far. As John3136 stated, what have you tried? Also consider making the wording of your question clearer. – John Carter Feb 20 '15 at 02:29

2 Answers2

1
#include<stdio.h>
#include<conio.h>
int main()
{
     int c,count=0,prev=' ';
     while((c=getchar())!=EOF)
     {
           if((prev==' '||prev=='\n'||prev=='\t')&&(c>='a'&&c<='z')||(c>='A'&&c<='Z'))
         count++;
        prev=c;
     }
     printf("%d",count);
}
Flicks Gorger
  • 195
  • 1
  • 1
  • 9
0

Your program acts like a finite state machine: it has a state and reads an input which determine the action to perform. To make things simpler, let's consider the following cases (assume current is the current character and last is the last character read):

  • current=letter and last=letter => do nothing
  • current=separator and last=letter => do nothing
  • current=separator and last=separator => do nothing
  • current=letter and last=separator

The last case is the interesting one. If you read a separator and then you read a letter, this means that you're starting to parse a new word, so you should increment word count. You need to think about the initial state of the program (what value does last have?).

Paul92
  • 8,827
  • 1
  • 23
  • 37