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

Naheed Edges reverse_words #39

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
43 changes: 42 additions & 1 deletion lib/reverse_words.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
# A method to reverse each word in a sentence, in place.
require 'pry'
def reverse_words(my_words)
raise NotImplementedError
i = 0
j = 0

if my_words == nil || my_words.length == 1 || my_words.length == 0
return
end

loop do
# binding.pry
until my_words[i] != " "
i += 1
end
j = i
until my_words[j + 1] == " " || j == (my_words.length - 1)
j += 1
end
# binding.pry
string_reverse(my_words, i, j)
i = j + 2
# binding.pry
break if i >= my_words.length - 1
end
return
end


def string_reverse(my_string, i, j)
if my_string == nil || i == j
return
end
temp = ""
temp2 = ""
while i < j do
temp = my_string[i]
temp2 = my_string[j]
my_string[j] = temp
my_string[i] = temp2
i += 1
j -= 1
end
return
end