Reverse words in a string: Difference between revisions

Line 1,907:
----------------------- Robert Frost</pre>
 
=={{header|MATLABMAXScript}}==
<lang maxscript>
<lang MATLAB>function testReverseWords
-- MAXScript : Reverse words in a string : N.H. 2019
testStr = {'---------- Ice and Fire ------------' ; ...
--
'' ; ...
(
'fire, in end will world the say Some' ; ...
text = stringstream "---------- Ice and Fire ------------\n\nfire, in end will world the say Some\nice. in say Some\ndesire of tasted I've what From\nfire. favor who those with hold I\n\n... elided paragraph last ...\n\nFrost Robert -----------------------\n"
'ice. in say Some' ; ...
clearListener()
'desire of tasted I''ve what From' ; ...
seek text 0
'fire. favor who those with hold I' ; ...
while eof text == false do
'' ; ...
(
'... elided paragraph last ...' ; ...
nextLine = (readLine text)
'' ; ...
if nextLine == "" then
'Frost Robert -----------------------' };
(
for k = 1:length(testStr)
print ""
fprintf('%s\n', reverseWords(testStr{k}))
continue
end
) -- end of if
revLine = ""
 
eachWord = filterString nextLine " "
function strOut = reverseWords(strIn)
for k = eachWord.count to 1 by -1 do
strOut = strtrim(strIn);
(
if ~isempty(strOut)
revLine = revLine + eachWord[k]
% Could use strsplit() instead of textscan() in R2013a or later
-- Only add space between words not at the end of line
words = textscan(strOut, '%s');
if k != 1 then revLine = revLine + " "
words = words{1};
) -- end of for k
strOut = strtrim(sprintf('%s ', words{end:-1:1}));
print revLine
end
) -- end of while eof
end</lang>
)
end</lang>
{{out}}
To MAXScript Listener:
<pre>------------ Fire and Ice ----------
testStr = {'<pre>"------------ IceFire and FireIce ------------' ; ..."
 
""
Some say the world will end in fire,
"Some say the world will end in ice.fire,"
"Some say in ice."
"From what I've tasted of desire"
"I hold with those who favor fire."
 
""
"... last paragraph elided ..."
 
""
"----------------------- Robert Frost"</pre>
 
=={{header|Modula-2}}==
Anonymous user