Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,103 people online right now. Registration is fast and FREE... Join Now!




word and sentence problem counters stumped me

 
Reply to this topicStart new topic

word and sentence problem counters stumped me

mcnasa01
23 Oct, 2006 - 10:52 PM
Post #1

New D.I.C Head
*

Joined: 25 Apr, 2006
Posts: 2


My Contributions
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

User is offlineProfile CardPM
+Quote Post

gregoryH
RE: Word And Sentence Problem Counters Stumped Me
24 Oct, 2006 - 12:12 AM
Post #2

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(mcnasa01 @ 23 Oct, 2006 - 11:52 PM) *

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.


I can see a lot of code all put in main, that is hard to read and even harder to fix...

by the way, what were you errors?

You only stated the purpose of the code.
User is offlineProfile CardPM
+Quote Post

mcnasa01
RE: Word And Sentence Problem Counters Stumped Me
24 Oct, 2006 - 08:05 AM
Post #3

New D.I.C Head
*

Joined: 25 Apr, 2006
Posts: 2


My Contributions
I'm not having any errors that's the funny part. The program works great counting sentences. The part I cannot figure out is how to make this code snippet not include spaces & tabs as words I had to put everything in main because we aren't allowed to use functions yet.

CODE

        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 determining if word has a space, tab, or eol

          //increase word count if a word has started and ended increase the word counter
           if(inWord == TRUE && endWord == TRUE) {
            inWord = FALSE;
            endWord = FALSE;
            wordCount++;
            printf(" %d\n", wordCount);
           }

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:03AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month