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

Recommended Answers

All 7 Replies

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

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

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.

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 :)

<<Moderator edit: Pointless flame-bait>>

can you post the code?

commented: -1E6 points for observation -1

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.