WC program

Good day to all,


So, I`m supposed to test this word counter program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

#include <stdio.h>


#define IN 1 
#define OUT 0

/* count lines, words and characters in input */

int main()
	
{
	int c, n1, nw, nc, state;
	
	state = OUT;
	n1 = nw = nc = 0;
	
	while (( c = getchar()) !=EOF) {
		++nc;
		if (c == '\n')
		++n1;
	
	    if ( c == ' ' || c == '\n' || c == '\t')
		    state = OUT;
		else if (state = OUT)  {
			state = IN;
			++nw;
		}
	}
	
	printf( "%d %d %d\n", n1, nw, nc);
	
}



My plan is to give it three kinds of input:


1. Valid Inputs.
2. Boundary Condition Inputs.
3. Invalid Inputs.

For part 1, I would feed it any text stream, including whitespace, newline and tab characters respectively.

When I try to compile this and feed it some characters and generate EOF I get this:

this is a string^D

I`m quite confused about this...
^D is EOF.
Hi,

Isn`t the program also supposed to print some output when EOF is hit?
Why do I need to press ctrl+D 2 times?
Last edited on
Run the program like so:

./a.out <<< "this is a string"

where a.out is your executable name
Let me suggest some boundary conditions that may be useful:
- empty file
- file with all white space
- file with no white space
- file with one word followed by white space
- file with white space followed by one word.

If the file doesn't end in '\n', then should the last line be counted? Put another way, if there are no \n characters in the file, does it have 1 line or 0?
Topic archived. No new replies allowed.