Number of Valid Words in a Sentence
A sentence is composed solely of lowercase letters ('a' to 'z'), digits ('0' to '9'), hyphens ('-'), punctuation marks ('!', '.', and ','), and spaces (' ') exclusively. Each sentence can be segmented into one or more tokens, with spaces (' ') acting as delimiters.
A token qualifies as a valid word under the following criteria:
- It comprises exclusively of lowercase letters, hyphens, and/or punctuation marks, excluding digits.
- It contains at most one hyphen '-'. If present, the hyphen must be surrounded by lowercase characters (e.g., "a-b" is valid, while "-ab" and "ab-" are not).
- It includes at most one punctuation mark, positioned at the token's end if present (e.g., "ab,", "cd!", and "." are acceptable, while "a!b" and "c.," are not).
Examples of valid words include "a-b.", "afad", "ba-c", "a!", and "!".
Given a string titled sentence, the task is to ascertain and print the count of valid words within the sentence.
Ada AI
I want to discuss a solution
Help me solve this
Give more examples
What's wrong with my code?
How to use 'for loop' in javascript?
javascript (node 13.12.0)