Ruby Word and Letter Counter

Ruby Word and Letter Counter

This simple Program will accept a string and return the total number of words and show the number of occurrences for each letter.


#Enter a string to see word & letter counts
puts "Enter your text:"
text = gets.chomp
puts  "#{text}"
word_count = text.split(' ').count
puts "Word count is #{word_count} \nLetter Frequency is:"
freqs = {}
freqs.default = 0
text.each_char { |char| freqs[char] += 1}
("a".."z").each {|x| puts "#{x} : #{freqs[x]}" }
puts "Thank You"

Click here to run this code

More Free Code Examples:

Try these Fun Games by Bobbie:

Play and Learn Russian

Battlestarship Game

Take a look at these Groovy Codes:

Fun IQ Test

Html Svg Starburst

Javascript Particles Fishes

Read the Latest Breaking Programming and Tech News, Great Articles and Tips:


  1. I really loved reading your blog. It was very well authored and easy to understand. Unlike other blogs I have read which are really not that good.Thanks alot! this


Post a Comment

Popular posts from this blog

Multi-tap Keypad Text Entry

Free Code Examples Directory