I need help figuring out how to count sentences from a .txt file.
The following guild lines are what is considered to be a sentence:
1. A TERMINATOR followed by two spaces
2. A TERMINATOR followed by one space and end-of-line
3. A TERMINATOR followed by end-of-line
4. A TERMINATOR followed by a double quote and a space
5. A TERMINATOR followed by a double quote and an end-of-line
terminators are '.', '!', '?'
the file i'm reading from is a test file that contains 4 sentences the problem I've found so far is that it's not recognizing '.', '!', '?' and just counting the end of lines and its counting 8 of them. I've spent 4 hours fiddling with the code could anybody give it a look and see what I'm doing wrong?
Body:
C is a general-purpose programming language. It has been closely associ-
ated with the UNIX system where is was developed, since both the system
and most of the programs that run on it are written in C. The language,
however, is not tied to any one operating system or machine; and although
it has been called a "system programming language" because it is useful
for writing compilers and operating systems, it has been used equally well
to write major programs in many different domains.
This file contains 91 words and four sentences!
(First half of code is for counting words, its working)
#include <stdio.h>
#include <ctype.h>
// Constants
#define HYPHEN '-'
#define SPACE ' '
#define MAX_STRING_SIZE 81
#define NEWLINE '\n'
#define QUOTE '"'
#define PER_TERM '.'
#define QUES_TERM '?'
#define EXE_TERM '!'
#define TRUE 1
#define FALSE 0
int main(void)
{
// Local Variables
char inputFileName[MAX_STRING_SIZE];
char curChar = '\0';
FILE *inputFileHandle = NULL;
int endOfFileResult = 0;
int inAWord = FALSE;
int inASentence = FALSE;
int isATerm = FALSE;
int secVal = FALSE;
int sentenceCount = 0;
int wordCount = 0;
// Begin
// Display Program Overview
printf("The following program will ask the user for a file and full and\n");
printf("path to file. Then it will count the number of words and\n");
printf("and sentences in the designated file.\n\n");
// Prompt user for input file and full path
printf("Enter input file and full path: ", inputFileName);
// Get input file name (with path)
gets(inputFileName);
// Try to open file
inputFileHandle = fopen(inputFileName, "r");
// If problem opening the inptut file then
if(inputFileHandle == NULL)
{
// Display error message about input file
printf("ERROR: File Names %s could not be opened!\n\n", inputFileName);
}// end if
else
{
// read first character
endOfFileResult = fscanf(inputFileHandle, "%c", &curChar);
// While loop for reading and counting words
while(endOfFileResult != EOF)
{
// If current character is a letter, number, - you're in a word
if (isalnum(curChar) || curChar == HYPHEN)
{
inAWord = TRUE;
}// end if
else if (inAWord && (curChar == SPACE || curChar == NEWLINE))
{
wordCount++;
inAWord = FALSE;
}//end else
// Get next character
endOfFileResult = fscanf(inputFileHandle, "%c", &curChar);
}//end while
// Goes back to the beginning of the file
rewind(inputFileHandle);
//read first character after rewind
endOfFileResult = fscanf(inputFileHandle, "%c", &curChar);
// While loop for reading and counting sentences
while(endOfFileResult != EOF)
{
//looks at first char to determine if alphanumeric if it is then
//inASentence to TRUE
if(isalnum(curChar) || curChar == HYPHEN)
{
inASentence = TRUE;
}// end if
//isAterm is set to TRUE if curChar is '?' '.' '!'
isATerm = curChar == QUES_TERM || PER_TERM || EXE_TERM;
// checks to if we are in a sentence and if the curChar is a TERM
// if both are true then isATerm is set to TRUE
if(inASentence && isATerm)
{
isATerm = TRUE;
}//end if
//Checks to see if isATerm is true and if the curChar is a newline
if(isATerm && curChar == NEWLINE)
{
sentenceCount++;
inASentence = FALSE;
}//end if
//Checks to see if isATerm is true and checks to see if curChar
//is a space or double quote if it is it set secVal to TRUE
else if(isATerm && (curChar == SPACE || curChar == QUOTE))
{
secVal = TRUE;
}//end if
//Checks to see if secVal is TRUE, if it's true then I looks at
//curChar to see if it is a space or newline then add 1 to
//sentenceCount
else if(secVal && (curChar == SPACE || curChar == NEWLINE))
{
sentenceCount++;
}// end if
// Get next character
endOfFileResult = fscanf(inputFileHandle, "%c", &curChar);
}// end while
}// end else
// Closes the file
fclose(inputFileHandle);
//Displays word and sentence counts.
printf("\nThere are %d words\n", wordCount);
printf("\nThere are %d sentences\n\n", sentenceCount);
return 0;
} // end function main
if just using the if statement to find newline and add 1 to sentence count the file should show 2.