tags:

views:

127

answers:

2

Developing an iPhone app which is a kind of notepad. How can I count words within a specific text string?

+3  A: 
 [[stringToCOunt componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet] count]
ennuikiller
`[[stringToCount componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet] count]`.
erikprice
thanks erikprice for pointing out the oversight!
ennuikiller
Fixed again.[[stringToCOunt componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] count]I was looking through documents by searching "counting words" or something like that, but I couldn't find a good way.This solution seems to be OK to me.Thank you all. (You guys are really quick!)
Nqeuew
This is not the most efficient way to count words. Specially not memory wise since it will split the whole string into a temporary array that will then be discarded. It is much better to simply look at runs of whitespace and punctiation in the text. This can't be done in one line but it will be much faster and will not use at least double the memory of the text.
St3fan
+3  A: 

A more efficient method than splitting is to check the string character by character.

int word_count(NSString* s) {
  CFCharacterSetRef alpha = CFCharacterSetGetPredefined(kCFCharacterSetAlphaNumeric);
  CFStringInlineBuffer buf;
  CFIndex len = CFStringGetLength((CFStringRef)s);
  CFStringInitInlineBuffer((CFStringRef)s, &buf, CFRangeMake(0, len));
  UniChar c;
  CFIndex i = 0;
  int word_count = 0;
  Boolean was_alpha = false, is_alpha;
  while (c = CFStringGetCharacterFromInlineBuffer(&buf, i++)) {
    is_alpha = CFCharacterSetIsCharacterMember(alpha, c);
    if (!is_alpha && was_alpha)
      ++ word_count;
    was_alpha = is_alpha;
  }
  if (is_alpha)
    ++ word_count;
  return word_count;
}

Compared with @ennuikiller's solution, counting a 1,000,000-word string takes:

  • 0.19 seconds to build the string
  • 0.39 seconds to build the string + counting using my method.
  • 1.34 seconds to build the string + counting using ennuikiller's method.

The big disadvantage of my method is that it's not a one-liner.

KennyTM
not a one-liner is a bit of an understatement!! :) The op didn't ask for the most efficient solution. I would suspect that most methods in the NSString class can be coded more efficiently. I guess the determining factor would be how large the "text string" is.
ennuikiller
Thanks Kenny! I was just asking a similar question and your answer is excellent! +1 ...i am gonna quietly borrow that code of yours.
Unikorn
The solution is (slightly) broken. Not all characters fit in one unichar.
JeremyP