In this article I would like to share with you a sample program that will ask the user to give a string or sentence and then our program will count the number of words in the given string or sentence using Ruby as our programming language. The code is very short and easy to understand.
I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My mobile number here in the Philippines is 09173084360.
I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
My mobile number here in the Philippines is 09173084360.
My telephone number at home here in Bacolod City, Negros Occidental is (034) 4335675.
Sample Program Output
Program Listing
word.rb
# word.rb
# Written By: Mr. Jake R. Pomperada, MAED-IT
# December 13, 2017
# Tool : Ruby and Sublime Text Editor
# Location : Bacolod City, Negros Occidental Philippines
# method to count the word in the sentence
def wordcount(str_value)
str_value.split(/\s+/).length
end
str_input =""
answer = ""
until answer.downcase == "n" do
print "\n\n"
print "===== WORD COUNTER IN RUBY ====="
print "\n\n"
print "Created by Mr. Jake R. Pomperada"
print "\n\n"
print "Give a string : "
str_input = gets.chop
total_words = wordcount(str_input)
print "\n\n"
puts "The total number of words in the given the sentence is #{total_words}."
print "\n"
print "Do you want to continue Y/N ? : "
answer= gets.chop
end
print "\n\n"
print "End of Program"
print "\n\n"