Skip to content

Instantly share code, notes, and snippets.

@manoj2411
Created April 25, 2015 15:59
Show Gist options
  • Save manoj2411/b072b7fb1406debc3820 to your computer and use it in GitHub Desktop.
Save manoj2411/b072b7fb1406debc3820 to your computer and use it in GitHub Desktop.
str = "the house is blue, it looks beautiful."
puts 'Initial string: ' + str
words_arr = []
i = 0
tmp_word = ''
while(i <= str.length)
if str[i] == ' '
words_arr.push tmp_word
tmp_word = ''
elsif str[i].nil?
tmp_word = tmp_word[0..-2] if tmp_word[-1] == '.'
words_arr.push tmp_word
else
tmp_word << str[i]
end
i += 1
end
str = ''
i = words_arr.length - 1
while i >= 0
str << words_arr[i]
str << " " if i != 0
i -= 1
end
str << '.' if str[-1] != '.'
puts 'Result string: ' + str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment