hi all I've tried for days to get this working but it just work won't my errors are.
1. the program counts spaces as sentences.
2. hyphenated words are counted as 1 word instead of 2
any help would be appreciated it.
CODE
// Compiler Includes
#include <stdio.h>
// Project Includes
// Constants
#define MAX_STRING_LENGTH 80
#define TRUE 1
#define HYWORD 2
#define FALSE 0
#define SPACE ' '
#define EOL '\n'
#define TAB '\t'
#define HYPHEN '-'
#define PERIOD '.'
#define EXCLAIM '!'
#define QUESTION '?'
int main(void) {
// Local Variables
char inputFileName[MAX_STRING_LENGTH] = {'\0'};
FILE *inputFileHandle = NULL;
char currentCharacter = '\0';
int endOfFileResult = 0;
int wordCount = 0;
int sentCount = 0;
int inWord = 0;
int endWord = 0;
int sentBegin = 0;
int sentEnd = 0;
// Begin
// get input file
printf("Enter file name: ");
gets(inputFileName);
printf("The file name entered is %s!\n", inputFileName);
//open input file
inputFileHandle = fopen(inputFileName, "r");
// If input file does NOT exist then
if (inputFileHandle == NULL) {
// THERE IS NO INPUT FILE!!!!
// Display error message to terminal
printf("ERROR: The file %s could not be found!\n", inputFileName);
} // end if
else {
// Else
// THERE IS AN INPUT FILE!!!
printf("Found the file!\n");
printf("The file contents are:\n");
printf("----------------------\n");
endOfFileResult = fscanf(inputFileHandle, "%c", ¤tCharacter);
// While not the end of file
while (endOfFileResult != EOF) {
// set up for word count
if (currentCharacter >= 'A' && currentCharacter <= 'Z' ||
currentCharacter >= '0' && currentCharacter <= '9' ||
currentCharacter >= 'a' && currentCharacter <= 'z' ||
currentCharacter == PERIOD || currentCharacter == QUESTION ||
currentCharacter == EXCLAIM){
inWord = TRUE;
printf("%c", currentCharacter); }
// end if for character scan
//determine if word finished if it has a space tab or EOL
if (inWord == TRUE && currentCharacter == SPACE || currentCharacter == TAB ||
currentCharacter == EOL) {
endWord = TRUE;
} // end if of word determine
//increase word count if a word has started and ended
if(inWord == TRUE && endWord == TRUE) {
inWord = FALSE;
endWord = FALSE;
wordCount++;
printf(" %d\n", wordCount);
}
//designate what a hyphened word is
if (currentCharacter == HYPHEN) {
inWord = HYWORD;
}//end if hyphen words
//if word is hyphened and not the EOL it is in a word
if (inWord == HYWORD && currentCharacter == EOL) {
inWord = TRUE;
}//end if
//if word is hyphened and not EOL increase word counter
else if (inWord == TRUE && currentCharacter == EOL) {
wordCount ++;
inWord = FALSE;
}//end else if
// Starts sentences
// determine if sentence starts with alpha numeric
if (currentCharacter >= 'A' && currentCharacter <= 'Z' ||
currentCharacter >= '0' && currentCharacter <= '9' ||
currentCharacter >= 'a' && currentCharacter <= 'z') {
sentBegin = TRUE;
sentEnd = FALSE;
} // end if
//determine what defines the end of a sentence
if (currentCharacter == PERIOD || currentCharacter == QUESTION
|| currentCharacter == EXCLAIM && currentCharacter != HYPHEN
&& currentCharacter != EOL) {
sentEnd = TRUE;
}//end if senence structure
// increase sentence counter if true after sentence begining
if (sentBegin == TRUE && sentEnd == TRUE) //&& currentCharacter == SPACE ||
currentCharacter == TAB || currentCharacter == EOL)
{
sentBegin = FALSE;
sentEnd = FALSE;
sentCount++;
printf(" %d\n", sentCount);
} //end if
//end of file
endOfFileResult = fscanf(inputFileHandle, "%c", ¤tCharacter);
} // end while
// Close input file
fclose(inputFileHandle);
// Display message that output file was created
printf("The number of words are %d.\n", wordCount);
printf("The number of sentences are %d.\n", sentCount);
printf("\n\nProgram has successfully ended!\n");
} // end else
} // end function main