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 have done an exercise on this. But it doesn't deal with this
condition: comp.lang.c - this group name will be treated as two
sentences. How can I improve it
$
$ type a.c
#include <stdio.h>
#include <ctype.h>
int wc2(const char *filename)
{
FILE *fp;
int ch;
int nc; /*num of chars*/
int nw; /*num of words*/
int ns; /*num of sentences*/
int inw; /*inside a word*/
nc = nw = ns = 0;
inw = 0;
if ((fp = fopen(filename, "r")) == NULL)
return -1;
while ((ch = fgetc(fp)) != EOF){
if (isalnum(ch)){
nc++;
inw = 1;
} else if ((ispunct(ch) || ch == ' ') && (inw == 1)){
nw++;
if (ch == '!' || ch == '?' || ch == '.' || ch == ';')
ns++;
inw = 0;
}
}
fprintf(stdout, "num of chars: %d\nnum of words: %d\nnum of
sentences: %d\n"
, nc, nw, ns);
fclose(fp);
return 0;
}
int main(int argc, char **argv)
{
if (argc != 2)
fprintf(stdout, "Usage: %s <filename>", argv[0]);
wc2(argv[1]);
return 0;
}
$ type test.txt
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).
--
Keith Thompson (The_Other_Keith) (e-mail address removed) <
http://www.ghoti.net/
~kst>
San Diego Supercomputer Center <*> <
http://users.sdsc.edu/
~kst>
"We must do something. This is something. Therefore, we must do
this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
$ a.out test.txt
num of chars: 533
num of words: 130
num of sentences: 19
$