From eafb95677f9cc5a7d84b7197d03e8200247f0bca Mon Sep 17 00:00:00 2001 From: Xtina Date: Tue, 4 Dec 2018 18:40:21 -0800 Subject: [PATCH 1/2] Create sherman-christina.rb --- sherman-christina.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 sherman-christina.rb diff --git a/sherman-christina.rb b/sherman-christina.rb new file mode 100644 index 0000000..38f1ad1 --- /dev/null +++ b/sherman-christina.rb @@ -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 From 9053df1d98814d64827eba1268ecbd9669a98a61 Mon Sep 17 00:00:00 2001 From: Xtina Date: Sat, 29 Dec 2018 21:50:39 -0800 Subject: [PATCH 2/2] Create Christina's-reverse-words.rb --- Christina's-reverse-words.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Christina's-reverse-words.rb diff --git a/Christina's-reverse-words.rb b/Christina's-reverse-words.rb new file mode 100644 index 0000000..bef7d04 --- /dev/null +++ b/Christina's-reverse-words.rb @@ -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