?
Solved

Reading Text file

Posted on 2005-02-28
3
Medium Priority
?
218 Views
Last Modified: 2010-04-15
I this program I have to open a text file  (comics.txt) and count the number of words, sentences and syllables.  

My problem is that I do not know how to read through the text to find sentence enders or syllables.

Code:
==================
#include <stdio.h>

#define STRING   30
#define      LENGTH   25
#define WORD     15
#define SYLLABLE  6


int
main (void)
{
      int total_sents, total_sylls, total_words, shift_ctr;
      double index;
      char sentence [STRING][LENGTH];
      char word [WORD];
      char syllable [SYLLABLE];
      FILE *inp;

      inp = fopen("a:\\comics.txt", "r");
      /* if the file will not open */
      if(inp == NULL){
            printf ("unable to open text readability file!!!\n");
            return(1);
      }


      printf("---------------\n");

      /*This is the loop to count the number of words in the document */

      total_words = 0;

      while (fscanf (inp, "%s",word) !=EOF);
      total_words += 1;

      /* I am unable to count the number of sentences ending in '.', '!', '?',
because I do not know how
      * to set up a loop to read through the text file.  This is also the reason
I cannot put in my
      * settings to count the number of syllables.  I understand that counting
the words invovles using the
      * %s (string) character, but I need help on reading through the document
and checking each
      *individual character for one of the endings above to see if it truly is
the end of a sentence */

      index = (206.835 - 84.6 * (total_sylls/total_words) - 1.015
                  * (total_words/total_sents));

      printf("---------------\n");


                  if((index >= 91) && (index <= 100)){
                              printf ("Index is %3f\n", index);
                              printf ("Text is at the following level:  5th Grader.\n");
                  }else if(index >= 81){
                              printf ("Index is %3f\n", index);
                              printf ("Text is at the following level:  6th Grader.\n");
                  }else if(index >= 71){
                              printf ("Index is %3f\n", index);
                              printf ("Text is at the following level:  7th Grader.\n");
                  }else if(index >= 66){
                              printf ("Index is %3f\n", index);
                              printf ("Text is at the following level:  8th Grader.\n");
                  }else if(index >= 61){
                              printf ("Index is %3f\n", index);
                              printf ("Text is at the following level:  9th Grader.\n");
                  }else if(index >= 51){
                              printf ("Index is %3f\n", index);
                              printf ("Text is at the following level:  High-school Student.\n");
                  }else if(index >= 30){
                              printf ("Index is %3f\n", index);
                              printf ("Text is at the following level:  College Student.\n");
                  }else if(index >= 0){
                              printf ("Index is %3f\n", index);
                              printf ("Text is at the following level:  College Graduate.\n");
                  }else{
                              printf ("Index is %3f\n", index);
                              printf ("Text is at the following level:  Law School Graduate.\n");
                  }


      }}}

      fclose(inp);

      printf("---------------\n");


      return (0);

}
===================

Sample Text File:
====================
Sherman's Lagoon: How about all these sheep on the island, Huh?
It's what shark dreams are made of.  Yep.  Have you guys had your
fill of sheep yet?
We're getting there.
Why did you start a second sheep?
I didn't see the other one.
Peanuts:
I read where someone said, if a man has the love of a dog, he is already a
millionaire.
And that'll buy a lot of cookies.
==================================
0
Comment
Question by:malikatwork
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13423943

It's pretty easy, really.  This looks like homework so I'll tell you what you need to do.

For the first cut, follow these simple rules.  :)


1)  read each line into a buffer.

2)  scan each line, examining every character.
     a)  if the character is alphabetic, increment the wordcount and skip to the first non-alaphabetic.
     b)  if the character is sentence ending punctuation (?!.) increment the sentence count and skip to the next alphabetic character.

3)  if end of line is reached, repeat from step 1.


Syllables is a whole other issue.  Tackle these easy ones first.
Kent
0
 

Author Comment

by:malikatwork
ID: 13424048
Could I get some sample code on how to read each line into a buffer and go through each character ?
0
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 13424278


Sure.....

FILE  *TextFile;
char  Buffer[200];
char  *ptr;

  TextFile = fopen ("comics.txt", "r");
  /*  sanity check that file opened ok  */

  while (1)
  {
    fgets (Buffer, 200, TextFile);
    if (feof (TextFile))
      break;
    cptr = Buffer;
    while (*cptr)
    {
       if (isapha (*cptr))             /* alphabetic charcter found, must be start of word  */
       {
          WordCount++;              /*  increment word count  */
          while (isapha (*cptr))    /*  Skip to character past end of word  */
            cptr++;
        }
        else if (*cptr == '.')         /*  period found  */
        {
           SentenceCount ++;
           while (*cptr && !isalpha (*cptr))
              cptr++;                     /*  skip all the fluff between the period and the next alphabetic character  */
        }
        else if (*cptr == '!')         /*  exclamation point found  */
/*  fill this in  */
        else if (*cptr == '?')        /*  question mark found  */
/*  fill this in, too  */
    }
  }


Note that this only solves the very basics of counting.  We don't care about numbers,  a0b0c0 would count as three words, etc.   abc-def counts as 2.

But once the basics are coded, we can adjust this to your needs quite easily.

Kent
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question