How to resolve the algorithm Reverse words in a string step by step in the Modula-2 programming language
How to resolve the algorithm Reverse words in a string step by step in the Modula-2 programming language
Table of Contents
Problem Statement
Reverse the order of all tokens in each of a number of strings and display the result; the order of characters within a token should not be modified.
Hey you, Bub! would be shown reversed as: Bub! you, Hey
Tokens are any non-space characters separated by spaces (formally, white-space); the visible punctuation form part of the word within which it is located and should not be modified. You may assume that there are no significant non-visible characters in the input. Multiple or superfluous spaces may be compressed into a single space. Some strings have no tokens, so an empty string (or one just containing spaces) would be the result. Display the strings in order (1st, 2nd, 3rd, ···), and one string per line. (You can consider the ten strings as ten lines, and the tokens as words.)
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Reverse words in a string step by step in the Modula-2 programming language
Source code in the modula-2 programming language
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.
You may also check:How to resolve the algorithm 24 game step by step in the Ruby programming language
You may also check:How to resolve the algorithm Product of min and max prime factors step by step in the MAD programming language
You may also check:How to resolve the algorithm Validate International Securities Identification Number step by step in the Nim programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the Haskell programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Verilog programming language