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
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
Related Q & A:
- What is the best way to distribute an audio/video feed from a computer to TVs over existing indoor coax cable?Best solution by Audio-Video Production
- What is the best way to count lines in file?Best solution by Stack Overflow
- What's the best way to hook up an overhead projector to a laptop?Best solution by Yahoo! Answers
- What's the best way to get a job in a restaurant?Best solution by Yahoo! Answers
- What is the best way to study for vocabulary words?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.