how to combine strings that are generated inside a function?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Roxanne Esguerra
am 22 Jul. 2020
Kommentiert: Roxanne Esguerra
am 23 Jul. 2020
Hi, I have this code that must randomly scramble the letters of the string input and return the result as a whole word. However, this returns the letters one by one.
function royalscramble(str)
exchange = randperm(length(str));
for i=1:length(str)
str(exchange(i))
end
end
I tried additions to the code and it worked. Here it is.
function scrambled = royalscramble(str)
exchange = randperm(length(str));
scrambled = '';
for i=1:length(str)
scrambled = str(exchange(i));
end
end
However, the function must not be called like that. I mean, it should not have scrambled =
It should be like this and returns this kind of result:
>> royalscramble('fantastic')
ans =
safntcait
>> royalscramble ('hello')
ans =
hleol
What should I add/replace from my first code? Thanks!
0 Kommentare
Akzeptierte Antwort
KSSV
am 22 Jul. 2020
function scrambled = royalscramble(str)
exchange = randperm(length(str));
scrambled = str(exchange) ;
end
4 Kommentare
Stephen23
am 22 Jul. 2020
"but I need a function that doesn't need "scrambled=" to be called... Is that possible?"
It is not required to call a function with an output argument:
But if you want any value returned, you will have to declar an output argument:
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!