MathGroup Archive 2009

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

Search the Archive

Re: Number of Words in a String


Gregory,

why don't you just use

In[1] =
wordNumber[x_String] := Length@StringCases[x, LetterCharacter ..];

In[2] =
wordNumber["The cat in the hat."]

Out[2] = 5

Regards,
Leonid


On Tue, Aug 11, 2009 at 12:02 PM, Gregory Lypny
<gregory.lypny at videotron.ca>wrote:

> Hello everyone,
>
> Is this the simplest way to find the number of words in a string?
> 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.
>
> Gregory
>
>


  • Prev by Date: Re: Number of Words in a String
  • Next by Date: Re: Number of Words in a String
  • Previous by thread: Re: Number of Words in a String
  • Next by thread: Re: Number of Words in a String