Question: MATLAB: Write a RECURSIVE function anagram that will receive a string as an input argument. It will then randomly scramble the letters and return the

MATLAB: Write a RECURSIVE function anagram that will receive a string as an input argument. It will then randomly scramble the letters and return the resulting anagram. The function should return an appropriate output regardless of the input string size (An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.)

Will only rate well for RECURSIVE matlab code, NOT allowed to use built-in Matlab functions, most specifically: perms, randperm, randsample.

Sample run:

>> anagram('Hello') ans = 

oHlle

This code doesn't allow the outstring to be the correct size and has a bunch of problem with indecies as the instring gets smaller

function outstring = anagram_rec(instring)

len = length(instring);

i = randi(len,1);

for j = 1:len

index = j;

outstring = char(len);

if index == 1

outstring(i) = instring(i);

instring(i) = [];

if len > 0

anagram(instring);

end

elseif index < len

outstring = [outstring,instring(i)];

instring(i) = [];

if len > 0

anagram(instring);

end

end

end

end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!