Need Some Help Formatting a Function Output Using Count

I'm using MATLAB to make a word scramble that scrambles the inner letters of every word in a sentence, leaving the first and last letter of each word in their respective places. I'm trying to figure out how to format this function so that if the user enters more than 10 words, the function will use a count to put the rest of the words on the next line down. Also, this function won't allow me to use two letter words (I assume because there is no "middle" letter in the word to pass through my function), and I was wondering how I can make my function work with two letter words. Here's what I have so far:
function outword= wordScramble(my_sentence)
while ~isempty(my_sentence)
[my_word,my_sentence] = strtok(my_sentence);
len = length(my_word);
first_letter = my_word(1);
last_letter = my_word(end);
input_word = my_word(2:end-1);
len = length(input_word);
in_vec = zeros(1,len);
in_vec(1) = randi([1 len]);
for i = 2:len
ran = randi([1 len]);
while any(in_vec(1:i-1) == ran)
ran = randi([1,len]);
end
in_vec(i) = ran;
end
inner_letters = input_word(in_vec);
outword = strcat(first_letter,inner_letters,last_letter);
fprintf('%s ',outword)
end
fprintf('\n')
end

3 Comments

dpb
dpb on 30 Apr 2020
Edited: dpb on 1 May 2020
"this function won't allow me to use two letter words"
The obvious answer there is test the length of the word and only work on those whose length is >3. The rule as given means nothing happens to those so the output for them is the same as the input. Unless you want to make it harder and substitute a different letter for the middle in the 3-letter case... :)
You can then do the permutation of the inner letters in one line by randperm.
I was thinking more along the lines of an if/else command. I don't have any experience with permute but I know I could use if and else to tell the function whether or not to scramble a word. I'm not sure where to put my if/else statements, and I don't know how I should word them in this case. Thanks for your response!
Well, think thru the problem...you start by getting a word, then the very next thing your code does is find out how long it is. Wouldn't right there be the place to make the decision on what to do depending on the length? Looks like to me... :)
BTW, if the length is four characters, there's only one permutation you can make and odds are 50:50 on a random choice you'll return the same sequence as you started with so you might want to special case it as well and just swap the two inner letters.
And, of course, with 5 or more, there's still a finite chance the random order could be the same as the initial altho odds go down pretty quickly as the number grows. Whether you want to build in the logic to make sure your return isn't the same is probably added credit...
>> input_word='machine';
>> len=numel(input_word);
>> input_word(2:end-1)=input_word(randperm(len-2))
input_word =
'mhmacie'
>>

Answers (0)

This question is closed.

Products

Release

R2020a

Asked:

on 30 Apr 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!