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

all test complete #19

Open
wants to merge 1 commit 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
50 changes: 48 additions & 2 deletions lib/reverse_words.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
# A method to reverse each word in a sentence, in place.
def reverse_words(my_words)
raise NotImplementedError
def reverse_letters(my_string)

return my_string if my_string.length == 1

while my_string != nil
i = 0
j = (my_string.length - 1)
while i < j
temp = my_string[j]
my_string[j] = my_string[i]
my_string[i] = temp
j -= 1
i += 1
end
return my_string
end
return nil
end

def reverse_words(my_string)

return my_string if my_string == nil

results = ""
i = 0
temp = ""
j = (my_string.length - 1)

while i < my_string.length
if i == j
temp += my_string[i]
results << reverse_letters(temp)
elsif my_string[i] != " "
temp += my_string[i]
else
results << reverse_letters(temp)
temp = ""
results += " "
end
i += 1
end

u = 0
while u < results.length
my_string[u] = results[u]
u += 1
end
return my_string
end