8 Replies - 4535 Views - Last Post: 18 April 2009 - 06:49 AM Rate Topic: -----

#1 Mohammed Mahmoud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-March 09

Counting the number of words in a string

Post icon  Posted 18 April 2009 - 05:48 AM

Hello can anyone tell me how to count the number of words in a string?
I think maybe i should count the number of spaces but how?
thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Counting the number of words in a string

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Counting the number of words in a string

Posted 18 April 2009 - 05:56 AM

View PostMohammed Mahmoud, on 18 Apr, 2009 - 04:48 AM, said:

Hello can anyone tell me how to count the number of words in a string?
I think maybe i should count the number of spaces but how?
thanks


What sort of string?
- C++ string?
or
- C-style character array?
Was This Post Helpful? 0
  • +
  • -

#3 Mohammed Mahmoud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-March 09

Re: Counting the number of words in a string

Posted 18 April 2009 - 05:57 AM

C++ string.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Counting the number of words in a string

Posted 18 April 2009 - 06:15 AM

There are probably 20 good ways to do this.

Here's one of them.

Set a counter to zero.
Set a locationMarker to zero.
Start a do-while() loop
___ Use find_first_of() to find the first instance of ' ' after the current value of locationMarker
___ If found increment the counter and set locationMarker to found position
______ If not found break out of the loop
End do-while() loop if locationMarker is at end of string

So now you have a counter value.
I'll leave it to you to work out if that is the number of words in the string or if more work is required.

The flaw in this approach is failure to correctly defend against double spaces between words in the string.
Can you come up with an approach that avoids that flaw?

Read here:
http://www.cplusplus.../find_first_of/

This post has been edited by janotte: 18 April 2009 - 06:20 AM

Was This Post Helpful? 1
  • +
  • -

#5 Mohammed Mahmoud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-March 09

Re: Counting the number of words in a string

Posted 18 April 2009 - 06:19 AM

Thanks a lot
Was This Post Helpful? 0
  • +
  • -

#6 Mohammed Mahmoud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-March 09

Re: Counting the number of words in a string

Posted 18 April 2009 - 06:28 AM

I was just going to ask what if the user entered more than one space between words in the string
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Counting the number of words in a string

Posted 18 April 2009 - 06:32 AM

View PostMohammed Mahmoud, on 18 Apr, 2009 - 05:28 AM, said:

I was just going to ask what if the user entered more than one space between words in the string


Perhaps you could think about the distance between the already found space and the next found space.
How big should the gap be to mean there is a word between them?
Was This Post Helpful? 0
  • +
  • -

#8 Mohammed Mahmoud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 27-March 09

Re: Counting the number of words in a string

Posted 18 April 2009 - 06:44 AM

What about checking on the first space and if there was another character (not a space) after it increment the counter if not no incrementing ?

This post has been edited by Mohammed Mahmoud: 18 April 2009 - 06:46 AM

Was This Post Helpful? 0
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Counting the number of words in a string

Posted 18 April 2009 - 06:49 AM

View PostMohammed Mahmoud, on 18 Apr, 2009 - 05:44 AM, said:

What about checking on the first space and if there was another character (not a space) after it increment the counter if not no incrementing ?


Sounds like a good idea to me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1