VB icon

A Simple Count String

Email
Submitted on: 1/1/2015 4:56:00 AM
By: Raymond Li (from psc cd)  
Level: Beginner
User Rating: By 6 Users
Compatibility: C
Views: 1790
 
     Aims to count how many words in the infile
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: A Simple Count String
// Description:Aims to count how many words in the infile
// By: Raymond Li (from psc cd)
//**************************************

/*****************************************/
/* Program Name : A Simple Count String */
/* Description : Aims to count how many */
/*words in the infile*/
/* Programmer: Raymond Li	 */
/*****************************************/
 
#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 256
main(int argc, char *argv[])
{
 char inword[MAX_LENGTH];
 int word_count = 0;
 FILE *infile;
 if (argc < 2)
 {
 printf("Format : wordcnt <filename>\n");
 exit( 1 );
 }
 /* open the infile */
 if ((infile = fopen( argv[1], "rt" )) == NULL)
 {
 printf("Cannot open input file - %s\n", argv[1] );
 exit( 1 );
 }
 /* read the string and count it while not end of file */
 while( !feof(infile) )
 {
 fscanf( infile, "%s", inword );
 word_count++;
 }
 
 /* print the result */
 printf( "There are %d word(s) in the file %s\n", word_count, argv[1] );
 /* close the infile */
 fclose(infile);
 return(0);
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.