Skip to content

Instantly share code, notes, and snippets.

@chiragmongia
Created July 31, 2013 06:55
Show Gist options
  • Select an option

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

Select an option

Save chiragmongia/6119898 to your computer and use it in GitHub Desktop.
#Question- Character Count - Ranges
#Write a method that returns the no of various lowercase, uppercase, digits and special characters used in the string. Make use of Ranges.
def character_count(input_string)
lower, upper, number, special_character = 0, 0, 0, 0
splitted_string = input_string.split(//)
for i in 0...splitted_string.size
case splitted_string[i]
when 'a'..'z'
lower += 1
when 'A'..'Z'
upper += 1
when '0'..'9'
number += 1
else
special_character += 1
end
end
"Lowecase- #{ lower } Uppercase- #{ upper } Digits- #{ number } Special Characters- #{ special_character }"
end
#Main
p "Enter the string"
input_string = gets.chomp
p character_count(input_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment