Question: MATLAB code, please follow instructions * exactly * A palindrome is a character vector ( e . g . , a word or number )
MATLAB code, please follow instructions exactly
A palindrome is a character vector eg a word or number which reads the same forwards or backwards. For, example, "racecar" is a palindrome since it is the same spelled
backwards; the number would also be a palindrome. A single character, egH would be the simplest palindrome.
a At the bottom of the script below, where indicated, complete the function, palit which will take input, which is a random character vector of any length. It will return an
output, which will be set to the following:l
a the character vector 'yes' if is a palindrome
b the first element where there is a mismatch, if it is not a palindrome.
For example, if were 'racecar', then would be set to 'yes'; if were ZxcvnvCxZ then would be set to since, after scanning from left to right, c is the first element
where there is a mismatch with its "mirror image" character, C
In your code, use a whileloop to compare the first and last characters, the second and nexttolast, etc., until it finds a mismatch or not You may need a counter to keep track
of the index of vector in the whileloop. Avoid any builtin functions except for length and floor
Hint: floor may be useful in stopping the while loop, since the length of the vector may be odd or even.
Note: even though is a character vector, its elements are indexed and compared in exactly the same way as a numerical vector.
b At the bottom of the script below, where indicated, complete the function, palrec which will take input, which is a random character vector of any length. Similarly to
palit it will return an output, which will be set to the following:
a the character vector 'yes' if is a palindrome
b the first element where there is a mismatch, if it is not a palindrome.
However, instead of using an iterative algorithm with a whileloop, palrec will use a recursive algorithm. It will not use a loop at all, but rather use ifstatements with a recursive
call, where appropriate. Avoid builtin functions, except for length; you may use colon notation, as this may be useful in the recursive call to palrec
Hint: as one strategy, in the function, you may want to compare the ends of the character vector, and in the recursive call, input a new vector that contains the second element
through the nexttolast, using colon notation. Thus, the "new" ends of the vector will then be compared.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
