Friday, August 23, 2013

Coderbyte: Word Count (Ruby)

PROBLEM

Have the function WordCount(str) take the str string parameter being passed and return the number of words the string contains (ie. "Never eat shredded wheat" would return 4). Words will be separated by single spaces.

SOLUTION

def WordCount(str) 
    return str.split(" ").count
end

print WordCount(STDIN.gets)