Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 (e.g., 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 "247742" would also be a palindrome. (A single character, e.g.,'H', would be the simplest palindrome.)
a) At the bottom of the script below, where indicated, complete the function, palit(), which will take input, x, which is a random character vector of any length. It will return an
output, y, which will be set to the following:l
a) the character vector 'yes' if x is a palindrome
b) the first element where there is a mismatch, if it is not a palindrome.
For example, if x were 'racecar', then y would be set to 'yes'; if x were 'ZxcvnvCxZ', then y would be set to x(3), 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 while-loop to compare the first and last characters, the second and next-to-last, etc., until it finds a mismatch (or not). You may need a counter to keep track
of the index of vector x in the while-loop. Avoid any built-in 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 x 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, x, which is a random character vector of any length. Similarly to
palit(), it will return an output, y, which will be set to the following:
a) the character vector 'yes' if x 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 while-loop, palrec() will use a recursive algorithm. It will not use a loop at all, but rather use if-statements with a recursive
call, where appropriate. Avoid built-in 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 next-to-last, using colon notation. Thus, the "new" ends of the vector will then be compared.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions