Determine if a string is collapsible: Difference between revisions
Content added Content deleted
(Add Ada) |
|||
Line 86: | Line 86: | ||
:* [https://rosettacode.org/wiki/Determine_if_a_string_is_squeezable determine if a string is squeezable]. |
:* [https://rosettacode.org/wiki/Determine_if_a_string_is_squeezable determine if a string is squeezable]. |
||
<br><br> |
<br><br> |
||
=={{header|Ada}}== |
|||
<lang Ada>with Ada.Text_IO; use Ada.Text_IO; |
|||
procedure Test_Collapsible is |
|||
procedure Collapse (S : in String) is |
|||
Res : String (1 .. S'Length); |
|||
Len : Natural := 0; |
|||
begin |
|||
Put_Line ("Input = <<<" & S & ">>>, length =" & S'Length'Image); |
|||
for I in S'Range loop |
|||
if Len = 0 or else S(I) /= Res(Len) then |
|||
Len := Len + 1; |
|||
Res(Len) := S(I); |
|||
end if; |
|||
end loop; |
|||
Put_Line ("Output = <<<" & Res (1 .. Len) & ">>>, length =" & Len'Image); |
|||
end Collapse; |
|||
begin |
|||
Collapse (""); |
|||
Collapse ("""If I were two-faced, would I be wearing this one?"" --- Abraham Lincoln "); |
|||
Collapse ("..1111111111111111111111111111111111111111111111111111111111111117777888"); |
|||
Collapse ("I never give 'em hell, I just tell the truth, and they think it's hell. "); |
|||
Collapse (" --- Harry S Truman "); |
|||
end Test_Collapsible; |
|||
</lang> |
|||
{{out}} |
|||
<pre>Input = <<<>>>, length = 0 |
|||
Output = <<<>>>, length = 0 |
|||
Input = <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>, length = 72 |
|||
Output = <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>, length = 70 |
|||
Input = <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length = 72 |
|||
Output = <<<.178>>>, length = 4 |
|||
Input = <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72 |
|||
Output = <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>, length = 69 |
|||
Input = <<< --- Harry S Truman >>>, length = 72 |
|||
Output = <<< - Hary S Truman >>>, length = 17</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |