Thread: finding no of words

  1. #1
    Registered User
    Join Date
    Apr 2010

    finding no of words

    can anybody please help me with a program to find the number of words in a given sentence , no matter how many spaces are inbetween.......
    Or atleast give me a clue.....

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    A clue? Sure... operator>> parses out data according to whitespace.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    You can use std::string::find_first_of() in a loop.

    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
    	string eg = "this and that";
    	size_t pos = 0;
    	while ((pos = eg.find_first_of(" \t",pos)) != eg.npos) {
    		cout << pos << eg.substr(pos) << endl;
    	return 0;
    This is just a clue, I'm not going to write the whole thing for you.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding Words within a Phrase
    By ipndrmath in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2009, 10:24 PM
  2. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  3. New Theme
    By XSquared in forum A Brief History of
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  4. using strlen and finding shortest and longest words
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-30-2001, 06:09 PM