Navigation
Synopsis Count words in a line.
Examples We count words using a regular expression match in a for loop. Each time that the pattern /[a-zA-Z0-9_]+/ matches, the body of the loop is executed and count is incremented.
module demo::common::WordCount::CountInLine1

public int countInLine1(str S){
  int count = 0;
  for(/[a-zA-Z0-9_]+/ := S){
       count += 1;
  }
  return count;
}
Let's try it:
rascal>import demo::common::WordCount::CountInLine1;
ok
rascal>countInLine1("Jabberwocky by Lewis Carroll");
int: 4
Is this page unclear, or have you spotted an error? Please add a comment below and help us to improve it. For all other questions and remarks, visit ask.rascal-mpl.org.