Write a function called ReverseLetters that takes an input phrase consisting of a single word and reverses the sequence of letters between the first letter and last letter. The input phrase could be a character vector of any length.

Respuesta :

Answer:

# The function reverseLetter is defined

def reverseLetter(received_word):

   # The first letter of the word is assigned to a variable using index 0

   firstLetter = received_word[0]

   # The last letter of the word is assigned to a variable using index of

   # string length - 1

   lastLetter = received_word[len(received_word) - 1]

   # reverse letters in between first and last letter is defined as

  # empty string

   reverseBetween = ""

   # A counter is defined to control the loop during the reversal

   # counter value is 2 from the length of the received string

   # The 2 is for the first and last letter remove

   counter = len(received_word) - 2

   # The while loop start

   while counter >= 1:

       # The reverseBetween string is concatenated with corresponding

      # index of received word

       # The index is from high to low since the process is string reversal

       reverseBetween += received_word[counter]

       # The value of counter is decremented

       counter -= 1

   # The reversed string is displayed with no separator

   print(firstLetter, reverseBetween, lastLetter, sep="")    

Explanation:

The code is well commented.

reverseLetter("come") will output cmoe

reverseLetter("welcome") will output wmoclee

reverseLetter("brainly") will output blniary

Answer:

sout = sin(end-1:-1:2);

sin(2:end-1) = sout

sout = sin

Explanation:

First, assign the middle letters of the word to the variable sout.

sout = sin(end-1:-1:2);

We want to reverse the middle letters, so by using "end-1:-1:2", we are storing every character starting from the second to last character to the second character in increments of one.

Next, replace the middle letters of the inputted string "sin" with the letters we just reversed in sout.

sin(2:end-1) = sout

This replaces every character from the second character to the second-to-last character. The first and last characters are left untouched.

Finally, because the program is looking for the final value of sout, assign what we stored into "sin" into sout.

sout=sin

So, your final answer should be

sout = sin(end-1:-1:2);

sin(2:end-1) = sout

sout = sin