MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Number of Words in a String

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102538] Re: Number of Words in a String
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Wed, 12 Aug 2009 04:37:42 -0400 (EDT)
  • References: <h5r8b5$l4q$1@smc.vnet.net>

Hi,

> 
> Is this the simplest way to find the number of words in a string?   

I don't know if you would consider this simpler, it is straight from the
documentation for StringCount:

StringCount["The cat in the hat.", WordCharacter ..]

> Seems a little complicated, and I can't seem to turn it into a  
> function because when I replace the string with the argument  
> placeholder myString_ I get an error message saying that a string is  
> expected in that spot.
> 
> 	Length[ReadList[StringToStream["The cat in the hat."], Word]]
> 
> 	Returns 5.

I don't know what you did when turning it into a function, but this:

numwords[s_String] := Length[ReadList[StringToStream[s], Word]]

numwords["The cat in the hat."]

seems to work alright....


hth,

albert


  • Prev by Date: Re: Number of Words in a String
  • Next by Date: Re: Bug: ListPlot and Tooltip
  • Previous by thread: Re: Number of Words in a String
  • Next by thread: Re: Re: Number of Words in a String