Created
July 31, 2013 08:04
-
-
Save chiragmongia/6120193 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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