473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Count Words

Hello,

I'm trying to develop a program that will enable me to count the number
of words in a text file. As a plus, I'd like to be able to count how
many different words there are too. I have a decent start on the
program, but am quite unsure of where to move from here. I need to
malloc space for the array, but am not sure how to. Also, I believe
that strlen may come into play. Although I've browsed similar topics,
I'm also still unsure of the loop formatting to actually count the
words.

Any help would be greatly appreciated.

Thanks,
James

#include <stdio.h>
#include <stdlib.h>
#define MAXWORDS 4000 //less than 4000 total words in the
//text file
char *word[MAXWORDS];
int wordcount[MAXWORDS];
#define MAXWLEN 30 //no words larger than 30 characters
char buff[MAXWLEN];
int nwords, totalwords;
main() {
int i;
while(get_word(buff)) {

/**** The part where I am stuck on ****/
}
for(i = 0; i < nwords; i++)
totalwords += wordcount[i]; //if I keep getting
//words, the loop will
//continue

printf("there were %d different words out of %d totalwords\n",
nwords, totalwords);
}

//-----ignore the section below, it defines what a word is to the
//-----program
I already have code to define to the compiler what a word is, so I
don't need help on that end. Therefore, I removed the code to save
space, which would've been at this location.

Nov 15 '05 #1
11 2407
Foodbank wrote:
Hello,

I'm trying to develop a program that will enable me to count the number
of words in a text file. As a plus, I'd like to be able to count how
many different words there are too. I have a decent start on the
program, but am quite unsure of where to move from here. I need to
malloc space for the array, but am not sure how to. Also, I believe
that strlen may come into play. Although I've browsed similar topics,
I'm also still unsure of the loop formatting to actually count the
words.

Any help would be greatly appreciated.

Thanks,
James

[snip]


It would be best if you used the hash_map container provided in the STL
package. Read the STL documentation for more help on hash_map. Also, STL
is C++, so if you do decide to take my advice into consideration, please
make it a point post any further questions to c.l.c++

Cheers,
Vimal.
--
If you would be a real seeker after truth, it is necessary that at least
once in your life you doubt, as far as possible, all things."
-- Rene Descartes
Nov 15 '05 #2
Thank you for your response, but it has nothing to do with what I'm
looking for. I don't even know what the hash_map container is. Also,
you stated it is for C++, I am using C.

Any more help is greatly appreciated.

Thanks,
James


Vimal Aravindashan wrote:
Foodbank wrote:
Hello,

I'm trying to develop a program that will enable me to count the number
of words in a text file. As a plus, I'd like to be able to count how
many different words there are too. I have a decent start on the
program, but am quite unsure of where to move from here. I need to
malloc space for the array, but am not sure how to. Also, I believe
that strlen may come into play. Although I've browsed similar topics,
I'm also still unsure of the loop formatting to actually count the
words.

Any help would be greatly appreciated.

Thanks,
James

[snip]


It would be best if you used the hash_map container provided in the STL
package. Read the STL documentation for more help on hash_map. Also, STL
is C++, so if you do decide to take my advice into consideration, please
make it a point post any further questions to c.l.c++

Cheers,
Vimal.
--
If you would be a real seeker after truth, it is necessary that at least
once in your life you doubt, as far as possible, all things."
-- Rene Descartes


Nov 15 '05 #3

Foodbank wrote:
Hello,

I'm trying to develop a program that will enable me to count the number
of words in a text file. As a plus, I'd like to be able to count how
many different words there are too. I have a decent start on the
program, but am quite unsure of where to move from here. I need to
malloc space for the array, but am not sure how to. Also, I believe
that strlen may come into play. Although I've browsed similar topics,
I'm also still unsure of the loop formatting to actually count the
words.


A school assignment I had to do exactly this was broken down into
several parts:
1. a tokenizer (get_word())
2. dynamic array support (insert_at(), get_at(), replace_at(),
append())
3. a hash table using the above.
4. the final program. hash each word, then you can count the number
of times it appears.

Nov 15 '05 #4
>I'm trying to develop a program that will enable me to count the number
of words in a text file.


Why? (other than to get a good grade on your homework assignment).

What is your definition of a "word"? How many words are there
on the following lines:

don't
3.141592
O'Brien
supercali-\nfragilisticexpialadocious
(where \n represents a newline and - represents a hyphen)
Nov 15 '05 #5
Vimal Aravindashan wrote on 26/09/05 :
It would be best if you used the hash_map container provided in the STL
package. Read the STL documentation for more help on hash_map. Also, STL is
C++, so if you do decide to take my advice into consideration, please make it
a point post any further questions to c.l.c++


How is any of this a response to a C-question ?

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
Nov 15 '05 #6

Gordon Burditt wrote:
I'm trying to develop a program that will enable me to count the number
of words in a text file.
Why? (other than to get a good grade on your homework assignment).

What is your definition of a "word"?


depends on what you pass to strcspn.
How many words are there on the following lines: assuming strcspn(p, "\n \t.,")
don't 1 3.141592 2 O'Brien 1 supercali-\nfragilisticexpialadocious
(where \n represents a newline and - represents a hyphen) 2 . 0 #&$(#&$(#

1

Nov 15 '05 #7
"tedu" <tu@zeitbombe.org> wrote:
Gordon Burditt wrote:
I'm trying to develop a program that will enable me to count the number
of words in a text file.


Why? (other than to get a good grade on your homework assignment).

What is your definition of a "word"?


depends on what you pass to strcspn.


No, it's precisely the other way around. What you pass to strcspn()
depends on how you define a "word".
How many words are there on the following lines:

assuming strcspn(p, "\n \t.,")

don't

1
3.141592

2


I say zero or one.
O'Brien

1
supercali-\nfragilisticexpialadocious
(where \n represents a newline and - represents a hyphen)

2


One, clearly, but only if you know that for a word.
.

0
#&$(#&$(#

1


None.

That's my definition; now _your_ job is to write a strcspn() that
matches it. Going the other way puts the cart before the horse. It's a
common error in programmers, and with my sysadmin hat on I would very
much like to eradicate it.

Richard
Nov 15 '05 #8
Hi everyone,

I've made some progress, but I'm getting incorrect word counts. Can
anyone check out my code and see what I might be doing wrong?

Thanks.
#include <stdio.h>
#include <stdlib.h>
#define MAXWORDS 4000
char *word[MAXWORDS];
int wordcount[MAXWORDS];
#define MAXWLEN 30
char buff[MAXWLEN];
int nwords, totalwords;
main() {
int i;
while(get_word(buff)) {

for(i = 0; i < nwords; i++)
if(!strcmp(buff, word[i]))
wordcount[i]++;

word[i] = (char *) malloc( strlen(buff) + 1);
strcpy(word[i], buff);
wordcount[i] = 1;
nwords++;
}
for(i = 0; i < nwords; i++)
totalwords += wordcount[i];
printf("there were %d unique words out of %d totalwords\n",
nwords, totalwords);
}

//*************I've deleted the code that tells the compiler what a
word is, I don't need help on that

Nov 15 '05 #9
Foodbank wrote:
while(get_word(buff)) {

for(i = 0; i < nwords; i++)
if(!strcmp(buff, word[i]))
wordcount[i]++;
word[i] = (char *) malloc( strlen(buff) + 1);
strcpy(word[i], buff);
wordcount[i] = 1;
i don't think you want to do the above three lines every time you see a
word.
nwords++;
}


it'd also help to make sure your indentation gets posted correctly.

Nov 15 '05 #10
Emmanuel Delahaye wrote:
Vimal Aravindashan wrote on 26/09/05 :
It would be best if you used the hash_map container provided in the
STL package. Read the STL documentation for more help on hash_map.
Also, STL is C++, so if you do decide to take my advice into
consideration, please make it a point post any further questions to
c.l.c++

How is any of this a response to a C-question ?


Read the OP's message again:

Foodbank wrote: Hello,

I'm trying to develop a program that will enable me to count the number
of words in a text file. As a plus, I'd like to be able to count how
many different words there are too. I have a decent start on the
program, but am quite unsure of where to move from here. I need to


In the problem statement the OP does not say that it has to be done in C
(Although, he has mentioned it his reply). The fact that he already has
a good start doesn't matter much if he is going to be stuck with
re-inventing the wheel. If it was up to me, then I would first get the
design right, and then figure out which language is best (unless there
is a constraint on the same, as in this case) to translate my design
into code. Moreover, the OP does say "any help" is welcome, so a
re-direction should really hurt much if it is going to save him quite
some time. ;-)

Cheers,
Vimal.
--
If you would be a real seeker after truth, it is necessary that at least
once in your life you doubt, as far as possible, all things."
-- Rene Descartes
Nov 15 '05 #11
"Foodbank" wrote:
I've made some progress, but I'm getting incorrect word counts. Can
anyone check out my code and see what I might be doing wrong?

Thanks.
#include <stdio.h>
#include <stdlib.h>
#define MAXWORDS 4000
char *word[MAXWORDS];
int wordcount[MAXWORDS];
#define MAXWLEN 30
char buff[MAXWLEN];
int nwords, totalwords;


Shouldn't you give those an initial value?

<snip>
Nov 15 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jester.dev | last post by:
Hello, I'm learning Python from Python Bible, and having some problems with this code below. When I run it, I get nothing. It should open the file poem.txt (which exists in the current...
22
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: ...
9
by: dan | last post by:
this is a program to count average letters per word. i am able to count the total number of letters, but not words. How do you count the total number of words in a text file, so i am able to divide...
9
by: cw bebop | last post by:
Hi all Using Visual Studio C# Have a string string st = "Hi, these pretzels are making me thirsty; drink this tea. Run like heck." ******
9
by: aaron | last post by:
I have a few documents in which I need to get a total word count. Can anyone help? I'd like to create this program so I can get the result after entering the filename, and have the option to...
3
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
4
by: jackloanshark | last post by:
Hello, I am a beginner of c. I wrote a program about counting words in a txt file.But actually there are some error in it and it will stop counting at the first paragraph and just return the number...
7
by: supriyanaidu | last post by:
Hi i am new to this forum . i am doing small project in that i want to count the number of words in the given .for example in my give file the content will be in this format Firstname, last...
3
by: frozz85 | last post by:
I need to search through the file and look for lines that begin with "From". I need to parse the From line and print out the second word for each From line and then also count the number of From...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.