Original Post
my program is supposed to count the number of non-blank characters, spaces and words in the text. In this program, a word is a series of consecutive letters, numbers, or special characters separated by one or more spaces on either side except at the beginning or end of a line. I am using a state machine. well everything works well except for one thing, there are supposed to be 11 words in the output and i get 12. not sure what is wrong but here is the code: #include #include #include using namespace std; void main() { char ch[80]; int i, spState, wrdState, sp = 0, nonBlank = 0, word = 0; ifstream fin; fin.open ("program13b.dat"); while (fin.getline(ch, 80)) { cout << ch << endl; i = 0; int lenght = strlen(ch); spState = true; wrdState = false; while (i < lenght) { if (spState && (ch == ' ' || ch == '\n')) {sp++; i++;} if (spState && (ch != ' ' || ch != '\n')) { spState = false; wrdState = true; word++; nonBlank++; i++; } if (wrdState && (ch != ' ' || ch != '\n')) {nonBlank++; i++;} if (wrdState && (ch == ' ' || ch == '\n')) { sp++; i++; spState = true; wrdState = false;} } } cout << "spaces = " << sp << endl; cout << "words = " << word << endl; cout << "non-blank = " << nonBlank << endl; fin.close(); }