caro1216
asked on
PROGRAM THAT COUNTS THE WORD OF A TXT FILE
Hi
I have been trying to code a program which counts the number of words in a text file. I know it is so easy but I have not expertise on this. I am a DBA and have not idea how to do it. Can anybody help me? Thank you so much
I have been trying to code a program which counts the number of words in a text file. I know it is so easy but I have not expertise on this. I am a DBA and have not idea how to do it. Can anybody help me? Thank you so much
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Carol,
You're going to have to make a few decisions before you start counting. Mostly, what constitues a "word". Is it alphabetic data only? Mixed alphabetic and numeric? Numeric data only? How do you count hyphenated words? Do you want to explicitly look for decimal data and count that as one word?
Lots of decisions......
But Stephan is right. You can start by simply writing a small program to examine, count, and skip based on the first character of each sequence, then modify it if necessary.
It's so nice to be able to help someone else when I can't quite get my head wrapped around my own Database issue. :) Here's some code to get you started...
main ()
{
int WordCount = 0;
char cc;
while (1) /* move to the first alphabetic character in the file */
{
cc = getch (stdin);
if (feof (stdin))
break; /* We've hit EOF and never found a character worth looking at */
if (isalpha (cc))
break;
}
while (!feof (stdin))
{
WordCount++;
while (!feof (stdin) && isalpha (cc)) /* skip past this word */
cc = getch (stdin);
while (!feof (stdin) && !isalpha (cc)) /* skip to the next word */
cc = getch (stdin);
}
printf ("%d words found.\n", WordCount);
}
Kent
The easiest way to do it is to let someone else do it:
long numWords = 0;
FILE fp = popen("wc -w myfile.txt");
if (fp) {
fscanf(fd, "%ld", &numWords);
pclose(fp);
}
The next easiest is to use appropriate standard library functions to do the work,
specifically strtok():
FILE * fd;
long numWords = 0;
static char buff[4096]; /* ??? if line > 4096 bytes, last word in buff may be cut in two */
/* open the file */
if (!(fd = fopen(argv[1], "r")))
return 1;
/* read the file line-by-line, tokenize each line, count number of words on each line */
while (fgets(buff, sizeof(buff), fd)) {
char *word;
static const char *sep = " \t\r\n\"()!?,.;:/[]{}+=@<> #*&^|`~";
for (word = strtok(buff, sep); word; word = strtok(NULL, sep))
numWords++;
}
fclose(fd);
long numWords = 0;
FILE fp = popen("wc -w myfile.txt");
if (fp) {
fscanf(fd, "%ld", &numWords);
pclose(fp);
}
The next easiest is to use appropriate standard library functions to do the work,
specifically strtok():
FILE * fd;
long numWords = 0;
static char buff[4096]; /* ??? if line > 4096 bytes, last word in buff may be cut in two */
/* open the file */
if (!(fd = fopen(argv[1], "r")))
return 1;
/* read the file line-by-line, tokenize each line, count number of words on each line */
while (fgets(buff, sizeof(buff), fd)) {
char *word;
static const char *sep = " \t\r\n\"()!?,.;:/[]{}+=@<>
for (word = strtok(buff, sep); word; word = strtok(NULL, sep))
numWords++;
}
fclose(fd);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
how about the standard Unix program "wc" - this will count bytes, characters, words and lines...
# to do all 3
wc filename
# to count words
wc -w filename
# to do all 3
wc filename
# to count words
wc -w filename
ASKER
wow.
thank you to everybody.That is helping me a lot. For now no UNIX, thanks, that will be my next assigment and i am going to keep an eye on it.
thank you to everybody.That is helping me a lot. For now no UNIX, thanks, that will be my next assigment and i am going to keep an eye on it.
ASKER