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

Christinas-reverse-words.rb #46

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions Christina's-reverse-words.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# A method to reverse each word in a sentence, in place.

def partial_reverse(my_string, starting, ending)
i = starting
j = ending
while i < j
temp = my_string[i]
my_string[i] = my_string[j]
my_string[j] = temp
i += 1
j -= 1
end
return
end

def reverse_words(my_words)
return if (my_words == nil || my_words.length == 0)

i = 0
total = my_words.length
while i < total
while my_words[i] == ' ' && i < total
i += 1
end
start_index = i

while my_words[i] != ' ' && i < total
i += 1
end
end_index = i - 1

partial_reverse(my_words, start_index, end_index)
end
return
end
14 changes: 14 additions & 0 deletions sherman-christina.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def string_reverse(my_string)
return nil if my_string == nil
mid = my_string.length / 2
last = my_string.length - 1
temp = ""
i = 0
while i < mid
temp = my_string[i]
my_string[i] = my_string[last - i]
my_string[last - i] = temp
i += 1
end
return temp
end