Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  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}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement