954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

word count

As you may already know im working on a program that analyses a file given by the user, i want to create a word count feature. How would i go about doing this, i mean how would i distinguish what is exactly is a word and be able to incorporate this into my code, im not specifically looking for any example code on this 1, more just ideas on the method, any links or tips could be useful. Thanks

bops
Posting Whiz in Training
214 posts since Aug 2005
Reputation Points: 23
Solved Threads: 5
 

Words are generaly seperated by spaces, so look for spaces between charaters.

zyruz
Junior Poster in Training
60 posts since Jun 2005
Reputation Points: 10
Solved Threads: 5
 

More precisely, look for spaces ONLY after characters.
That will help you a great deal with tabs and incorrect spacing.

Drowzee
Posting Whiz in Training
245 posts since Jul 2005
Reputation Points: 22
Solved Threads: 5
 

Do you want to do this in C or C++. For C++ see the code snippet at:
http://www.daniweb.com/code/snippet225.html

With C you have to slug it out in the trenches! Not too hard to do, just labor! A word can end with a space, tab, period, comma, semicolon, newline, ? or ! and so on. Simply count all the isalpha(characters) that are followed by on of those endings.

vegaseat
DaniWeb's Hypocrite
Moderator
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Thanks a lot guys, I actually thought of spaces at first but then as i though deeper i got a bit confused on how i would write the code, but it is done now and works fine. I decided to store the file in an array and check if the character proceeding the 1 in question was a space, tab or newline, and then counted this as a word, i have also tried to take into account a word at the end of the file that may not have a leading character. This may not be so accurate but it works fine on all of my tests i have done. Thanks for the tips guys, if anyone wants me to post teh code just let me know in this thread :D

I was using c++ by the way (Dev-C++) vegaseat, and what are you doing up so early lol :p oh wait different time zone, my bad lol :)

bops
Posting Whiz in Training
214 posts since Aug 2005
Reputation Points: 23
Solved Threads: 5
 
vegaseat
DaniWeb's Hypocrite
Moderator
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

can you post the code?

Kcin
Newbie Poster
16 posts since Feb 2007
Reputation Points: 9
Solved Threads: 1
 

>can you post the code?
No, because this thread is two years old.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You