Determine if a string is collapsible: Difference between revisions

Add Modula-2
(Added 11l)
(Add Modula-2)
Line 1,943:
PASSES 1 out of 1 test
</pre>
 
=={{header|Modula-2}}==
<lang modula2>MODULE StrCollapse;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
FROM Strings IMPORT Length;
 
(* Collapse a string *)
PROCEDURE Collapse(in: ARRAY OF CHAR; VAR out: ARRAY OF CHAR);
VAR i, o: CARDINAL;
BEGIN
i := 0;
o := 0;
WHILE (i < HIGH(in)) AND (in[i] # CHR(0)) DO
IF (o = 0) OR (out[o-1] # in[i]) THEN
out[o] := in[i];
INC(o);
END;
INC(i);
END;
out[o] := CHR(0);
END Collapse;
 
(* Display a string and collapse it as stated in the task *)
PROCEDURE Task(s: ARRAY OF CHAR);
VAR buf: ARRAY [0..127] OF CHAR;
PROCEDURE LengthAndBrackets(s: ARRAY OF CHAR);
BEGIN
WriteCard(Length(s), 2);
WriteString(" <<<");
WriteString(s);
WriteString(">>>");
WriteLn();
END LengthAndBrackets;
BEGIN
LengthAndBrackets(s);
Collapse(s, buf);
LengthAndBrackets(buf);
WriteLn();
END Task;
 
BEGIN
Task("");
Task('"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ');
Task("..1111111111111111111111111111111111111111111111111111111111111117777888");
Task("I never give 'em hell, I just tell the truth, and they think it's hell. ");
Task(" --- Harry S Truman ");
END StrCollapse.</lang>
{{out}}
<pre> 0 <<<>>>
0 <<<>>>
 
72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>
 
72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
4 <<<.178>>>
 
72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
69 <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>
 
72 <<< --- Harry S Truman >>>
17 <<< - Hary S Truman >>></pre>
 
=={{header|Nim}}==
2,094

edits