Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pushpa_Agr_C10_reverse_words #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion lib/reverse_words.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,51 @@
require'pry'
# A method to reverse each word in a sentence, in place.
def reverse_words(my_words)
raise NotImplementedError

if my_words == nil
return nil
end


length = (my_words.length - 1)
i = 0
while i < length

while my_words[i] == " "
i = i + 1
end

start = i

while my_words[i] != " "
i = i + 1
break if my_words[i].nil?
end

end_i = i - 1

reverse_word(my_words, start, end_i)

end

return my_words

end



def reverse_word(my_words, start, end_i)

first = my_words[start] #b
last = my_words[end_i] #e

while start < end_i
my_words[start] = last
my_words[end_i] = first
start = start + 1
end_i = end_i - 1
first = my_words[start]
last = my_words[end_i]
end
return
end