SHARE
TWEET

Untitled

a guest Apr 25th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # get a string input
  2. def get_string
  3.     puts "Enter a string"
  4.     gets.chomp
  5. end
  6.  
  7. # start splitting each words
  8. def process(str)
  9.     result = {}
  10.     words = str.split(' ')
  11.     words.each do |word|   
  12.         result[word] = alternate_capitalize(word)
  13.     end
  14.     result.values.join(' ')
  15. end
  16.  
  17. # each word in the string calling alternate capitalize
  18. def alternate_capitalize(word)
  19.     result = []
  20.     word.chars.each_with_index do |c, index|
  21.         if index.even?
  22.             result << c.downcase
  23.         else
  24.             result << c.upcase
  25.         end
  26.     end
  27.     result.join('')
  28. end
  29.  
  30. str = get_string
  31. result = process(str)
  32. puts "alternate_capitalize for the given string is \n#{result}"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top