This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Count number of words in a file

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How count the number of words in a file? They dont have a specific format so I know I would have to count as I go but im not sure what I would use to do that.
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Hanna,

do you know how to recognize a word in the first place? Can you elaborate a little?
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example the file could look like this:


 
Piet Souris
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well,

the general concept is:
read the file line by line
split every line on the basis of one or more spaces
count how many elements you get per line
and sum these counts.

Now, it is more than 25 years ago since I worked with C, so some specialist around here can tell you how to do that.

But what I meant in my first reply: is '&&&' to be considered as a word?
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes the &&& would count as a word as well
 
Rancher
Posts: 332
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use scanf() with format %s: "String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab)."

From https://www.tutorialspoint.com/c_standard_library/c_function_scanf.htm
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would that work if it was coming from a file? Isn't scanf for user input?Do you think I could use getc() to do it? also what kind of conditions should I have to count it correctly since there can be multiple spaces/whitespace?
 
John Matthews
Rancher
Posts: 332
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like printf() has fprintf() to output to file, scanf() has fscanf() to read from a file.

Multiple spaces don't make a word, so if there are multiple spaces, they aren't going to be in the 'words' that (f)scanf() reads. Try it.

Yes, you could use getc() if you want to do it the hard way
 
John Matthews
Rancher
Posts: 332
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only need 2 lines of code to read and count your words using fscanf() (plus variable definitions and final output).

(That's using a 'while' loop.)
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dont you need to know how many words are on each like for fscanf? or would it just put it into a big string for each line that way?
 
John Matthews
Rancher
Posts: 332
11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

John Matthews wrote:Try it.



You don't need to worry about lines (see description of scanf() %s which I quoted earlier). All you do need to worry about is that the string variable which you read your words into is big enough to hold the longest word that you are going to read (suggest using something like 1000 char).

And you must test the return value of the fscanf() to check whether it actually has read a word or not - if it hasn't then you can assume you've reached the end of the file.
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok ill try it out thanks
 
Saloon Keeper
Posts: 7108
165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On a Unix/Linux/MacOS machine, I'd use ProcessBuilder (or Runtime.exec if you prefer that) to run "wc". On a Windows Machine, you can do pretty much the same.
 
lowercase baba
Posts: 13003
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hanna Roberts wrote:For example the file could look like this:


For future reference...examples are great, however you should really try and write a spec.  You want to write out the exact rules that take into account every possible scenario.  That's what Piet wrote in his post after yours...it is unambiguous as to what counts as a word and what doesn't (at least, I can't think of a case it doesn't account for).  With your example, it certainly helped, but wasn't definitive.
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah I get that, it was mainly because the instructions I was given for the assignment were pretty vague so when I wrote the example I wasn't exactly sure if symbols and things were to be counted as words. I'll try to be more specific in the future though, thanks
 
Hanna Roberts
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:On a Unix/Linux/MacOS machine, I'd use ProcessBuilder (or Runtime.exec if you prefer that) to run "wc". On a Windows Machine, you can do pretty much the same.



Yeah when I got the project that was what I thought I was supposed to use but they wanted it done in an actual function so I couldn't use the wc command
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic