Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: How to count the word(s) that you inputted?

Status
Not open for further replies.

Kwolf

Programmer
Mar 8, 2003
1
PH
I'm an aspiring programmer. I know I have a lot to learn, that’s why I’m starting to solve some problem my co-worker asked me.

Here's what i know, these counts the length of the string:
#include<iostream.h>
#include<string.h>
#include<stdio.h>
void main()
{
char str[80];
cout << &quot;Count the length of the string&quot; << endl;
cout << &quot;Enter string: &quot; << endl; gets(str);
cout << &quot;The length is: &quot; << strlen(str) << endl;
}

But what if I want to count by words not by length, what should I do?

Please give me advice, or tips, or reference books about this subject. Thanks in advance.
 
You could count the spaces in the string.
Greetings,
Rick
 
I guess there's no built in function for this (C++ doesn't have many high level string torturing functions in the standard library) so you'll probably have to code your own.

The count-spaces-approach sounds good but won't work if there are multiple spaces between words. You could go through the string char for char and whenever theres whitespace, you can be sure that the next alphanumeric char starts a new word (that goes on until theres whitespace).
Counting spaces is, of course, way faster and easier.
 
If you find this sort of thing interesting, there is an immense discussion of word-counting in Michael Abrash's big black book. It's utterly fascinating. But provided you are only counting words in something you typed, then using the speediest method possible isn't too relevant, so scanning by character and counting a word for each transition from space back to alpha-numeric is as good a way to count words as any. But do think about the ends of your string, and make sure you count the first and last words the same whether or not the user types a space before the first word or after the last.
 
You could try using STL as follows:
Code:
//Do not use the old .h headers

#include <string>
#include <iostream>
#include <algorithm>

using namespace std; //bad form, but used for code clarity

int main() //main should return int
{
 string input;
 cout << &quot;Count the number of words in a string&quot; << endl;
 cout << &quot;Enter the string: &quot; ;
 getline(cin, input);
 int pos = 0;
 int pos2 = 0;
 int word_count = 0;
 string space = &quot; &quot;;
 while(pos != string::npos)
 {
  pos = input.find_first_not_of(space, pos)
  if(pos != string::npos)
  {
   pos2 = input.find_first_of(space, pos)
   pos - pos2;
   ++word_count;
  }
 }
 cout << &quot;The word count is &quot; << word_count << endl
 return 0;
}
 
Regarding Federal102s post:

I think you have a typo or bug in there, look at this line:
Code:
pos - pos2;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top