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

Recommended Answers

All 3 Replies

Here is a staring point.
You started to use the inAWord but didn't take it all of the way. Think of it like in a word, not in a word, in a word, not in a word, etc... changing the value of inAWord as you go.
Also there are some functions you might want to look at:
http://ieng9.ucsd.edu/~cs30x/Std.C/ctype.html
I modified you code to use some of the functions in that page.

// Compiler Includes
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_STRING_SIZE 81
#define TRUE 1
#define FALSE 0

int main(void)
{
  // Local Variables
  char	inputFileName[MAX_STRING_SIZE];
  char	curChar = '\0',*tmp;
  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: ");

  // Get input file name (with path)
  fgets(inputFileName,MAX_STRING_SIZE,stdin);
  if ( (tmp = strchr(inputFileName,'\n') )){
      *tmp = '\0';
  }
  // Replaced with fgets, because my compiler didn't like it
  //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 this is a printable character and it is not white space
         // then we are in a word
         if (isprint(curChar) && !isspace(curChar)){
            inAWord = TRUE;
         } 
         // If we get here and we were just in a word
         // then this must be the end of the word
         else if ( inAWord ){
            wordCount++;
            inAWord = FALSE;
         }
         // Get the next character
         endOfFileResult = fscanf(inputFileHandle, "%c", &curChar); 
      }//end while
   }// end else
   fclose(inputFileHandle);
   printf("\nThere are %d words\n", wordCount);
   return 0;
} // end function main

// end file program5.c

I haven't thought through the sentenceCount too much but the issue with that is you are reading one character at a time. And to know the end of a sentence you have to have some punctuation followed by two spaces. So, leaving things the same (reading one character at a time) you would have to have a inASentence variable that keeps track just as the inAWord did and not just rely on the next character but a sequence of 3 character. Just keep this in mind when starting the sentence part.

>> So far my code is counting all multi-letter words but not single letter words

Try changing "a" to "ab" or any other multi-letter word. I see no difference in the count. Your problem is that you have used one test case and that test case has erroneously led you to believe that the problem is something that it is not. You need to throw more things at this program till you see a pattern and hence the problem. For example, I made a file with nothing but spaces and got a word count of four, which is clearly incorrect.

Thanks for the help histrungalot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.