Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # get a string input
- def get_string
- puts "Enter a string"
- gets.chomp
- end
- # start splitting each words
- def process(str)
- result = {}
- words = str.split(' ')
- words.each do |word|
- result[word] = alternate_capitalize(word)
- end
- result.values.join(' ')
- end
- # each word in the string calling alternate capitalize
- def alternate_capitalize(word)
- result = []
- word.chars.each_with_index do |c, index|
- if index.even?
- result << c.downcase
- else
- result << c.upcase
- end
- end
- result.join('')
- end
- str = get_string
- result = process(str)
- puts "alternate_capitalize for the given string is \n#{result}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement