Reverse words in a string: Difference between revisions

Line 1,892:
 
=={{header|Modula-2}}==
{{trans|Pascal}}
{{works with|ADW Modula-2|any (Compile with the linker option ''Console Application'').}}
<lang modula2>
MODULE ReverseWords;
 
FROM STextIO IMPORT
WriteString, WriteLn;
FROM Strings IMPORT
Assign, Concat, Append;
 
CONST
NL = CHR(10);
Sp = ' ';
Txt = "---------- Ice and Fire -----------" + NL +
NL +
"fire, in end will world the say Some" + NL +
"ice. in say Some" + NL +
"desire of tasted I've what From" + NL +
"fire. favor who those with hold I" + NL +
NL +
"... elided paragraph last ..." + NL +
NL +
"Frost Robert -----------------------" + NL;
 
TYPE
String400 = ARRAY [0 .. 399] OF CHAR;
 
PROCEDURE AddWord(Source: ARRAY OF CHAR; VAR INOUT Destination: ARRAY OF CHAR);
VAR
R: String400;
BEGIN
Concat(Source, Sp, R);
Append(Destination, R);
Assign(R, Destination);
END AddWord;
 
VAR
I: CARDINAL;
SingleWord, CurrentLine: String400;
C: CHAR;
 
BEGIN
SingleWord := "";
CurrentLine := "";
FOR I := 0 TO HIGH(Txt) DO
C := Txt[I];
CASE C OF
Sp:
AddWord(SingleWord, CurrentLine);
SingleWord := ""; |
NL:
AddWord(SingleWord, CurrentLine);
WriteString(CurrentLine);
WriteLn;
SingleWord := "";
CurrentLine := ""; |
ELSE
Append(C, SingleWord);
END;
END;
END ReverseWords.
</lang>
 
=={{header|Nim}}==
Anonymous user