I'm current trying to write codes that will read a .txt file and count the words and sentences. Right now im currently just trying to count words. So far my code is counting all multi-letter words but not single letter words. I know i can hard code in 'a' and 'i' (both cap and lowercase) but i want to know if there was a way around that.
text file:
Name: test.txt
Body:
This is a test file.
wordCount only returns 4 words
Current Code:
// Compiler Includes
#include <stdio.h>
// Constants
#define CAP_LET_MIN 'A'
#define CAP_LET_MAX 'Z'
#define LOW_LET_MIN 'a'
#define LOW_LET_MAX 'z'
#define DIGIT_MIN '0'
#define DIGIT_MAX '9'
#define HYPHEN '-'
#define SPACE ' '
#define DOUB_SPACE ' '
#define TAB '\t'
#define MAX_STRING_SIZE 81
#define NEWLINE '\n'
#define PERIOD_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 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!", inputFileName);
}// end if
else
{
// read first character
endOfFileResult = fscanf(inputFileHandle, "%c", &curChar);
// While not at the end of the file//////////////////////////////////////
while(endOfFileResult != EOF)
{
// If current character is a letter, number, - you're in a word
if (curChar == HYPHEN+NEWLINE || curChar >= CAP_LET_MIN ||
curChar <= CAP_LET_MAX || curChar >= LOW_LET_MIN ||
curChar <= LOW_LET_MIN || curChar >= DIGIT_MIN ||
curChar <= DIGIT_MAX)
{
inAWord = TRUE;
endOfFileResult = fscanf(inputFileHandle, "%c", &curChar);
if(curChar == SPACE || curChar == DOUB_SPACE ||
curChar == NEWLINE)
{
wordCount++;
}// end if (word count)
}//end if
}//end while
}// end else
printf("\nThere are %d words\n", wordCount);
return 0;
} // end function main
// end file program5.c