How to Count total number of Words in PDF?

What is the best way to count the number of words in a sentence?

  • I wanted to write a C++ program that takes a block of text from the user and shows the number of words in that block. What is the best (i.e., foolproof) way to count the number of words in that block of text? "Foolproof" means a human would count the same number of words as the program in all cases. Are there several ways to do so? How does Microsoft Word, or (say) http://750words.com do it?

  • Answer:

    I dont know exactly,But still we can count no...

Ashok Kumar at Quora Visit the source

Was this solution helpful to you?

Other answers

#include<iostream> #include <string> using namespace std; int main() { string content; int index; int wordcount = 1; getline(cin, content); for( index = 1; index < content.length(); index++ ) { if( content[i] == ' ' && content[i - 1] != ' ' ) { wordcount++; } } cout << wordcount; return 0; }

Sagar Gupta

This can be made as arbitrarily complex as you'd like due to the "in a sentence" portion of your question.  Parsing out what defines inter-sentence-gaps can be a bear - trust me. However, once you had a string/stream that contains the sentence content only, the basic algorithm's super simple - all you really need to do is to count the gaps between words, and add one, or use a languages capability to split a string into words, using spaces/tabs/cr/lf/whatever as separators, and ask how large the resulting collection of sub-strings is. posted a comment on Sagar's C++ answer showing how this can be done with one line of Python code.  It can similarly be done with just a little perl, or awk even.

Paul Reiber

#include <iostream> #include <sstream> using namespace std; int main() { string line, word; getline(cin, line); stringstream ss(line); int word_count = 0; while(getline(ss, word, ' ')) word_count += word.length() ? 1 : 0; cout << word_count; return 0; } Edit : Thanks to Vivek Nagarajan for pointing out this possible flaw. Using bool directly in arithmetic may not always be a good idea as some compilers might not have 'true' set to 1.

Venkatesh Ganesan

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.