From b92745aab3a8fbe49752e1fbd0b20e2a6c70099a Mon Sep 17 00:00:00 2001 From: Mariana Date: Thu, 28 Sep 2017 17:31:33 -0700 Subject: [PATCH 1/3] Exercise 1 and 2 --- string_manipulation.rb | 62 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index cf49bd9..62421bd 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,16 +1,72 @@ # A method to reverse a string in place. def string_reverse(my_string) - puts "NOT IMPLEMENTED" + i = 0 + j = my_string.length-1 + temp = my_string[i] + while i < j do + temp = my_string[i] + my_string[i] = my_string[j] + my_string[j] = temp + i += 1 + j -=1 + end end # A method to reverse each word in a sentence, in place. def reverse_words(my_words) - puts "NOT IMPLEMENTED" + i = 0 + j = 0 + while i < my_words.length do + while my_words[j] != " " && j < my_words.length do + j += 1 + end + last = j-1 + first = i + while first < last do + temp = my_words[first] + my_words[first] = my_words[last] + my_words[last] = temp + first += 1 + last -= 1 + end + i = j+1 + j += 1 + end + return my_words end # A method to reverse the words in a sentence, in place. def reverse_sentence(my_sentence) - puts "NOT IMPLEMENTED" + # firstfirst = 0 + # firstlast = 0 + # lastfirst = my_sentence.length - 1 + # lastlast = my_sentence.length - 1 + # + # while firstlast < lastfirst do + # while my_sentence[firstlast] != " " + # firstlast += 1 + # end + # + # while my_sentence[lastlast] != " " + # lastfirst -= 1 + # end + # + # firstword = my_sentence[firstfirst ... firstlast] + # lastword = my_sentence[lastfirst+1 .. lastlast] + # + # + # my_sentence = lastword + " " + my_sentence[firstlast + 1 .. lastfirst - 1 ] + " " + firstword + # + # firstlast = lastword.length + 1 + # lastfirst = my_sentence.length - (firstword.length + 2) + # firstfirst = firstlast + # lastlast = lastfirst + # + # end + # + # return my_sentence + + end # A method to check if the input string is a palindrome. From 2708198917588805298419a3dc1650d71a9f1d03 Mon Sep 17 00:00:00 2001 From: Mariana Date: Thu, 28 Sep 2017 18:01:10 -0700 Subject: [PATCH 2/3] Exercise 3 --- string_manipulation.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index 62421bd..74b0678 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -65,7 +65,10 @@ def reverse_sentence(my_sentence) # end # # return my_sentence - + string_reverse(my_sentence) + reverse_words(my_sentence) + return my_sentence + end From c10665ed6516df272bf2f56d38de56326be21042 Mon Sep 17 00:00:00 2001 From: Mariana Date: Thu, 28 Sep 2017 23:16:13 -0700 Subject: [PATCH 3/3] Exercise 4 --- string_manipulation.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/string_manipulation.rb b/string_manipulation.rb index 74b0678..a3685bb 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -10,6 +10,7 @@ def string_reverse(my_string) i += 1 j -=1 end + return my_string end # A method to reverse each word in a sentence, in place. @@ -65,18 +66,20 @@ def reverse_sentence(my_sentence) # end # # return my_sentence - string_reverse(my_sentence) - reverse_words(my_sentence) - return my_sentence - - + # string_reverse(my_sentence) + reverse_words(string_reverse(my_sentence)) + # return my_sentence end # A method to check if the input string is a palindrome. # Return true if the string is a palindrome. Return false otherwise. def palindrome_check(my_phrase) - puts "NOT IMPLEMENTED" - return true + # puts string_reverse(my_phrase) + if my_phrase.dup == string_reverse(my_phrase) + return true + else + return false + end end # A method that updates the string by replacing consecutive repeating characters