count method

int count(
  1. String text
)

Implementation

int count(String text) {
  int wordCount = 0;
  bool lastCharWasPunctuation =
      true; // Start as true to count first word if it starts with normal char

  // Iterate through each character using String.characters for proper Unicode support
  for (final char in text.characters) {
    bool isCurrentCharPunctuation = punctuationChars.contains(char);

    // Count a word when we transition from punctuation to normal character
    if (!isCurrentCharPunctuation && lastCharWasPunctuation) {
      wordCount++;
    }

    lastCharWasPunctuation = isCurrentCharPunctuation;
  }

  return wordCount;
}