diff --git a/string_manipulation.rb b/string_manipulation.rb index cf49bd9..a3685bb 100644 --- a/string_manipulation.rb +++ b/string_manipulation.rb @@ -1,23 +1,85 @@ # 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 + return my_string 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 + # 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