def replace_ending(sentence, old, new):
S1 = sentence
O1 = old
N1 = new
# Check if the old string is at the end of the sentence
if O1 in S1:
# Using i as the slicing index, combine the part
# of the sentence up to the matched string at the
# end with the new string
i = S1.rsplit(' ',1)[0] + str(" ") + N1
new_sentence = i
return new_sentence
# Return the original sentence if there is no match
return sentence
print(replace_ending("It's raining cats and cats", "cats", "dogs"))
# Should display "It's raining cats and dogs"