Count total no. of characters,words & sentences in a text file

R

Richard Heathfield

Umesh said:
Please try to do it while I try myself!

Look what happened the last time you cross-posted to clc and clc++.

Pick a language - any language - but pick ONE language, and post to the
newsgroup appropriate to that language.

The problem to which you refer is one of the easier K&R examples, and
should cause you no difficulty.
 
O

osmium

Umesh said:
Please try to do it while I try myself!

Men don't talk much about doing "it".

Doing the counts you mention would probably be easier if you wrote three
separate functions, that way you only have one train of thought at a time.
 
D

Default User

Umesh said:
Please try to do it while I try myself!

Quit cross-posting between groups. I don't think you will get much help
if you continue this anti-social behavior.

Figure out which language you are working in, then post to that group.



Brian
 
L

Lew Pitcher

Please try to do it while I try myself!

Already done, and (IIRC) published.

We'll be happy to take a look at your solution, any time you wish to
post it
 
U

Umesh

#include<stdio.h>
int main(void)
{
long int ch,c,num=0,num1=0,num2=0,num3=0;
FILE *f;
f=fopen("c:/1.txt","r");
while((ch=getc(f))!=EOF && (ch=getc(f))!=EOF)
{
if(ch==' ') ++num;
if(ch=='.') ++num1;
if(ch<=256) ++num2;
if(ch=='.' && c!=' ') ++num3; /* '.' followed by '.' denotes end of a
sentence.*/
}
printf("\nNo. of spaces = %ld",num);
printf("\nNo. of full stops = %ld",num1);
printf("\nNo. of characters = %ld",num2);
printf("\nNo. of characters without spaces = %ld",num2-num);
printf("\nNo. of sentences = %ld",num3);
return 0;
}
 
O

osmium

:

Just a couple of random observations.
#include<stdio.h>
int main(void)
{
long int ch,c,num=0,num1=0,num2=0,num3=0;

Your choice of names is not very helpful.
FILE *f;
f=fopen("c:/1.txt","r");
while((ch=getc(f))!=EOF && (ch=getc(f))!=EOF)
{
if(ch==' ') ++num;
if(ch=='.') ++num1;
if(ch<=256) ++num2;
if(ch=='.' && c!=' ') ++num3; /* '.' followed by '.' denotes end of a

I doubt you wanted '=' in there.
In the US, a sentence is usually indicated by ". " , two spaces.
sentence.*/
}
printf("\nNo. of spaces = %ld",num);
printf("\nNo. of full stops = %ld",num1);
printf("\nNo. of characters = %ld",num2);
printf("\nNo. of characters without spaces = %ld",num2-num);
printf("\nNo. of sentences = %ld",num3);
return 0;
}

This looks like a draft. I would not think you were happy with it because
of the = above. When you get happy with it, try to improve it. For
example, handle hyphenated words, at least the simple cases. I don't think
they can all be resolved without a dictionary. Following Google rules for
words seems fine to me. AFAIK, they have no rules for sentences.
 
I

Ian Collins

Umesh said:
#include<stdio.h>
int main(void)
{
long int ch,c,num=0,num1=0,num2=0,num3=0;
FILE *f;
f=fopen("c:/1.txt","r");
while((ch=getc(f))!=EOF && (ch=getc(f))!=EOF)

You never learn, do you?
 
B

Bill Pursell

Please try to do it while I try myself!

This works for one case.

#include <stdio.h>

#define FORMAT "Characters: %s\tWords: %s\tSentences: %s\n"
#define ZERO "0"
#define GT "at least one"
#define UNK "unknown"

int main( void )
{
int c;

if( EOF == getchar())
printf( FORMAT, 0, 0, 0 );
else
printf( FORMAT, GT, UNK, UNK );
}
 
D

Default User

[clc++ remove]
#include<stdio.h>
int main(void)
{
long int ch,c,num=0,num1=0,num2=0,num3=0;
FILE *f;
f=fopen("c:/1.txt","r");

How many times have you been told to check for failure when using
fopen()? Why should we bother telling you anything if you won't listen?




Brian
 
U

Umesh

Please try to do it while I try myself!

#include<stdio.h>
int main(void)
{
long int ch,c,num=0,num1=0,num2=0,num3=0;
FILE *f;
f=fopen("c:/1.txt","r");
while((ch=getc(f))!=EOF && (ch=getc(f))!=EOF)
{
if(ch==' ') ++num;
if(ch=='.') ++num1;
if(ch<=256) ++num2;
if(ch=='.' && c!=' ') ++num3; // '.' followed by '.' denotes end of a
sentence.
}
printf("\nNo. of spaces = %ld",num);
printf("\nNo. of full stops = %ld",num1);
printf("\nNo. of characters = %ld",num2);
printf("\nNo. of characters without spaces = %ld",num2-num);
printf("\nNo. of sentences = %ld",num3);
return 0;
}
 
U

Umesh

//Sorry my teachers. I've included file opening check in this program.

#include<stdio.h>
int main(void)
{
long int ch,c,num=0,num1=0,num2=0,num3=0;
FILE *f;
f=fopen("c:/1.txt","r");
if (f!=NULL)
{
while((ch=getc(f))!=EOF && (ch=getc(f))!=EOF)
{
if(ch==' ') ++num;
if(ch=='.') ++num1;
if(ch<=256) ++num2;
if(ch=='.' && c!=' ') ++num3; // '.' followed by '.' denotes end of a
sentence.
}
printf("\nNo. of spaces = %ld",num);
printf("\nNo. of full stops = %ld",num1);
printf("\nNo. of characters = %ld",num2);
printf("\nNo. of characters without spaces = %ld",num2-num);
printf("\nNo. of sentences = %ld",num3);
}
else printf("Error in opening file c:/1.txt ");
return 0;
}
 
K

Keith Thompson

Umesh said:
#include<stdio.h>
int main(void)
{
long int ch,c,num=0,num1=0,num2=0,num3=0;
FILE *f;
f=fopen("c:/1.txt","r");
while((ch=getc(f))!=EOF && (ch=getc(f))!=EOF)
{
if(ch==' ') ++num;
if(ch=='.') ++num1;
if(ch<=256) ++num2;
if(ch=='.' && c!=' ') ++num3; /* '.' followed by '.' denotes end of a
sentence.*/
}
printf("\nNo. of spaces = %ld",num);
printf("\nNo. of full stops = %ld",num1);
printf("\nNo. of characters = %ld",num2);
printf("\nNo. of characters without spaces = %ld",num2-num);
printf("\nNo. of sentences = %ld",num3);
return 0;
}

This program has numerous problems, which I'll be happy to discuss
with you if you pick *one* newsgroup to post to (if that newsgroup
happens to be comp.lang.c; I don't regularly read comp.lang.c++).
C and C++ are two different languages, and cross-posting between
comp.lang.c and comp.lang.c++ is almost never a good idea.

But the first thing you should do is to run the program and take a
look at its output (hint: the results it reports are incorrect).
 
I

Ian Collins

Umesh said:
//Sorry my teachers. I've included file opening check in this program.

#include<stdio.h>
int main(void)
{
long int ch,c,num=0,num1=0,num2=0,num3=0;

Use meaningful names and declare them in the scope where they are used.
FILE *f;
f=fopen("c:/1.txt","r");

Whitespace is free, use it. This will only work on a system where
"c:/1.txt" is a valid filename and where "c:/1.txt" exists...
if (f!=NULL)
{
while((ch=getc(f))!=EOF && (ch=getc(f))!=EOF)

Why are you doing this?
{
if(ch==' ') ++num;
if(ch=='.') ++num1;
if(ch<=256) ++num2;
if(ch=='.' && c!=' ') ++num3; // '.' followed by '.' denotes end of a
sentence.

If you are going to add comments, make sure they make sense and agree
with the code.
 
J

John Bode

#include<stdio.h>
int main(void)
{
long int ch,c,num=0,num1=0,num2=0,num3=0;

Use meaningful names, for both your sanity and ours; if num is meant
to count characters, indicate that with a name like numChars; if num1
is meant to count stops, name it numStops. Also, what is the purpose
of c, other than to introduce a bug later on?
FILE *f;
f=fopen("c:/1.txt","r");
while((ch=getc(f))!=EOF && (ch=getc(f))!=EOF)

Why are you reading two characters at a time? You're only going to
wind up checking every other character in the text file. I'm
reasonably sure that's not what you want to do.
{
if(ch==' ') ++num;
if(ch=='.') ++num1;
if(ch<=256) ++num2;
if(ch=='.' && c!=' ') ++num3; // '.' followed by '.' denotes end of a
sentence.}

What about sentences that end with '?' or '!'? What about
abbreviations or titles like Mr. or Dr.? And when did you assign
something to c?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top