
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...