Count words from an input file via file redirection 
Author Message
 Count words from an input file via file redirection

I have this program that has to count the combination of letters of (on)
from an inputfile via file redirection.
And when it reads the letter combination (end) thats the EOF.
Does any one know what i am doing wrong in this program?

please help will be much appreciated.

/*Word counter*/

#include <stdio.h>

int main ()
{
    int c;
    int letter_count= 0;

    while (c = getchar()) {
   if ((c == 'e') || (c == 'E'))
   if ((c == 'n') || (c == 'N'))
   if ((c == 'd') || (c == 'D'))

    break;

 else if ((c == 'o') || (c == 'O'))

  if ((c =='n') || (c =='N')){
     ++letter_count;
  }

    }

    /* Display the number of words found */
    printf("There were %d occurrences of the letter combination
\"on\"\n",letter_count);

    return (0);

Quote:
}



Mon, 15 Aug 2005 12:56:26 GMT  
 Count words from an input file via file redirection

"Marvin Odor" wrote

Quote:
> I have this program that has to count the combination of letters of (on)
> from an inputfile via file redirection.
> And when it reads the letter combination (end) thats the EOF.
> Does any one know what i am doing wrong in this program?

You're only comparing one char at a time, which makes it
difficult to pick up words.   As a hint, one way to accomplish
that (while still reading a character at a time) is to simply
keep boolean flags.

A brief code snippet:

ch = getc();
if ((ch == 'O') || (ch == 'o')) foundO = 1;
if (foundO && ((ch == 'N') || (ch == 'n')) count++;
else foundO = 0;

That can also be done with a switch statement.

Of course, you need to also decide what meets the criteria and
set flags accordingly.   For example, " on " meets the criteria
(probably) but " don't " (probably) doesn't.   There's plenty of
"special" cases to consider.

There are far easier ways to accomplish the same task.  Another
way to do it is to read data into an array and use strncmp().
And so on...



Mon, 15 Aug 2005 17:04:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Finding the minimum number of a txt file of numbers using input redirection

2. Counting #/words in text file

3. Can't open database file unless filed count matches

4. WORD Automation via C#:BuiltInProperties in Word

5. How to write rtf file of a word file using VC++

6. How to read a MS Word file ( .doc ) file from C program

7. Difference in size of RTF files across Word 97 and Word 2000

8. How to read a file word by word?

9. input file is an .avi file

10. File redirection

11. system() redirection to a file?

12. Problem with input stream redirection in lex

 

 
Powered by phpBB® Forum Software