Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

Program that will accept a line of text from the text file; count the number of words, lines, and characters in the file

Posted By: Guilherme Silva     Category: C Programming     Views: 11164

Write a program that will accept a line of text from the text file; count the number of words, lines, and characters in the file; and then output these numbers to the standard output. For simplicity, assume that either a single or more blanks, or a new-line or end-of-file character delimits words. A new-line character delimits lines.

Code for Program that will accept a line of text from the text file; count the number of words, lines, and characters in the file in C Programming

#include <stdio.h>
#include <conio.h>

void getReadFileName(char *str);
void countWordLine();

  FILE *fp;
  char filename[14];

  void main()
  {
    clrscr();
    getReadFileName(filename);
    countWordLine();

    getch();
  }

   void getReadFileName(char *str)
   {
     printf("Enter Input File name maxium 14 character : ");
     scanf("%s", str);

     if( ( fp = fopen(str, "r+t") ) == NULL  )
     {  fclose(fp);
    printf("\nInvalid file name entered.");
     }
   }

   void countWordLine()
   {
      int wordcount=0;
      int linecount=0;
      char prechr, curchr;

      while( feof(fp) == 0)
      {
     curchr = getc(fp);

     if( curchr == '\n')
     {
         if( prechr != '.' && isspace(prechr) == 0)
        wordcount++;
         linecount++;
     }
     elseif( isspace(curchr) > 0 || curchr == '.')
     {
        if(  ! ( (prechr == '.') && isspace(curchr) ) )
        {
           if( isspace(prechr) == 0 && isspace(curchr) != 0 )
          wordcount++;
           elseif (curchr == '.')
         wordcount++;
        }
     }
     prechr = curchr;
      }
      printf("\nTotal numbers of words in the file is  : %d", wordcount);
      if( linecount != 0)
        printf("\nTotal numbers of lines in the file is  : %d", ++linecount);
      else
        printf("\nTotal numbers of lines in the file is  : %d", linecount);

   }


    /**************************    Input / Output ************************/
Enter Input File name maxium 14 character : demo.txt Total numbers of words in the file is : 35 Total numbers of lines in the file is : 10
  
Share: 



Guilherme  Silva
Guilherme Silva author of Program that will accept a line of text from the text file; count the number of words, lines, and characters in the file is from Salvador, Brazil.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!