<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>word count - C++ - tribe.net</title>
  <link rel="alternate" href="http://cpp.tribe.net/thread/5192bf64-ceb1-4c6c-9002-0673b1ffe292?format=atom" />
  <subtitle>Tribe.net. Local Connections</subtitle>
  <entry>
    <title>Re: word count</title>
    <link rel="alternate" href="http://CPP.tribe.net/thread/5192bf64-ceb1-4c6c-9002-0673b1ffe292#3cbdca5f-98ff-4d23-94d9-e7acbdf25eb6" />
    <author>
      <name>Hypno</name>
    </author>
    <id>http://CPP.tribe.net/thread/5192bf64-ceb1-4c6c-9002-0673b1ffe292#3cbdca5f-98ff-4d23-94d9-e7acbdf25eb6</id>
    <updated>2006-05-01T20:06:24Z</updated>
    <published>2006-05-01T20:06:24Z</published>
    <summary type="html">Sounds like homework. :)&#xD;
&#xD;
Just off the top of my head, (no warranties implied).  You need to know when a word starts and stops.  This does a basic job but doesn't catch special cases.  I've allowed letters and numbers to be considered as part of a word and anything else as a delimeter.  So while this counts Fred69 and 42 as words it would erroneously count 3.14 as two words unless a special case for a period following a number was added.  foo@bar.com would not parse right either come to think of it.  But you could end up with a really huge function to try and catch every case.  If I had an anal retentive teacher I would also write an input function that did not allow chars I did not want to handle.&#xD;
&#xD;
I based this on the look of the code you were going for but you could also parse out the ending points of words using the strstr() function with a list of delimiting characters.  Anyways, I'm fairly certain this compiles and probably even works.&#xD;
&#xD;
#include "ctype.h"&#xD;
int	CountIt(char *str)&#xD;
{&#xD;
	char	ch;&#xD;
	int		words		= 0;&#xD;
	bool	wordstarted	= false;&#xD;
&#xD;
	do&#xD;
	{&#xD;
		ch = *str++;&#xD;
		if (isalnum(ch))	// ignore punctuation, use isalpha if you don't want to allow numbers in words or numbers as words.&#xD;
		{&#xD;
			wordstarted	= true;&#xD;
		}&#xD;
		else&#xD;
		{&#xD;
			if (wordstarted)&#xD;
			{&#xD;
				wordstarted	= false;&#xD;
				++words;&#xD;
			}&#xD;
		}&#xD;
	} while(ch);&#xD;
&#xD;
	return words;	&#xD;
}</summary>
    <dc:creator>Hypno</dc:creator>
    <dc:date>2006-05-01T20:06:24Z</dc:date>
  </entry>
  <entry>
    <title>word count</title>
    <link rel="alternate" href="http://CPP.tribe.net/thread/5192bf64-ceb1-4c6c-9002-0673b1ffe292#6899df8d-2dc5-458b-8baf-21eb230f12fb" />
    <author>
      <name>two</name>
    </author>
    <id>http://CPP.tribe.net/thread/5192bf64-ceb1-4c6c-9002-0673b1ffe292#6899df8d-2dc5-458b-8baf-21eb230f12fb</id>
    <updated>2006-05-01T18:18:50Z</updated>
    <published>2006-05-01T18:18:50Z</published>
    <summary type="html">I wrote a program counting word from a string a user inputs but i am having a hard time figuring out how to go back and count the number of characters in each word and then avreaging that out.  any help anyone can provide would be appreciated.  &#xD;
Thanks&#xD;
my program goes as follows&#xD;
#include &amp;amp;lt;iostream&gt;&#xD;
#include &amp;amp;lt;cctype&gt;&#xD;
using namespace std;&#xD;
&#xD;
int wordCount(char * , char);&#xD;
int word;&#xD;
&#xD;
char userString;&#xD;
&#xD;
int main()&#xD;
{&#xD;
	const int size = 81;&#xD;
	char userString[size];&#xD;
	cout &amp;amp;lt;&amp;lt; "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"&amp;amp;lt;&amp;amp;lt;endl;&#xD;
	cout &amp;amp;lt;&amp;amp;lt;endl&amp;amp;lt;&amp;lt; "Enter a string of up to 80 characters:  "&amp;amp;lt;&amp;amp;lt;endl&amp;amp;lt;&amp;amp;lt;endl;&#xD;
	cin.getline(userString, size);&#xD;
	cout &amp;amp;lt;&amp;lt; " The number of words in that string are: ";&#xD;
	cout &amp;amp;lt;&amp;lt; wordCount(userString,size) &amp;amp;lt;&amp;amp;lt;endl;&#xD;
	cout &amp;amp;lt;&amp;lt; " The average words in this string are:  ";&#xD;
	cout &amp;amp;lt;&amp;lt; word&amp;amp;lt;&amp;amp;lt;endl;&#xD;
	return 0;&#xD;
}&#xD;
&#xD;
&#xD;
int wordCount(char *strPtr , char word)&#xD;
{&#xD;
	int index =1;&#xD;
	while(*strPtr != '\0')&#xD;
	{&#xD;
		if(*strPtr == ' ' )&#xD;
			index++;&#xD;
			strPtr++;&#xD;
&#xD;
	}	&#xD;
	return index;</summary>
    <dc:creator>two</dc:creator>
    <dc:date>2006-05-01T18:18:50Z</dc:date>
  </entry>
</feed>



