To count each word in a given string in Ruby, you can use the following steps:
ta're
should be counted as one word.Here's an example implementation:
main.rb254 chars13 lines
Running this code will output 4
, as there are four words in the given string: wa
, ta're
, ba
, and 100
.
Please note that this implementation counts numbers as words as requested. If you want to exclude numbers from the word count, you can modify the regular expression used in the gsub
method.
gistlibby LogSnag