Skip to content

Instantly share code, notes, and snippets.

@chiragmongia
Created July 31, 2013 08:04
Show Gist options
  • Select an option

  • Save chiragmongia/6120193 to your computer and use it in GitHub Desktop.

Select an option

Save chiragmongia/6120193 to your computer and use it in GitHub Desktop.
# Question- Occurence - Hash
#Count the ocurrences of various alphabet letters in an input string and store it in hash.
def alphabet_count_and_store_in_hash(hash, input_string)
input_string.each_char { |char| hash[ char ] += 1}
hash.delete_if {|k,v| k !~ /^[a-zA-Z]+$/ }
end
# Main
hash = Hash.new(0)
p "Enter the string"
input_string = gets.chomp
alphabet_count_and_store_in_hash(hash, input_string)
hash.each {|key| p key}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment