Jump to content

Determine if a string is squeezable: Difference between revisions

Add Ada
(Add Ada)
Line 102:
:*   [https://rosettacode.org/wiki/Determine_if_a_string_is_collapsible determine if a string is collapsible].
<br><br>
 
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Squeezable is
procedure Squeeze (S : in String; C : in Character) is
Res : String (1 .. S'Length);
Len : Natural := 0;
begin
Put_Line ("Character to be squeezed: '" & C & "'");
Put_Line ("Input = <<<" & S & ">>>, length =" & S'Length'Image);
for I in S'Range loop
if Len = 0 or else (S(I) /= Res(Len) or S(I) /= C) then
Len := Len + 1;
Res(Len) := S(I);
end if;
end loop;
Put_Line ("Output = <<<" & Res (1 .. Len) & ">>>, length =" & Len'Image);
end Squeeze;
begin
Squeeze ("", ' ');
Squeeze ("""If I were two-faced, would I be wearing this one?"" --- Abraham Lincoln ", '-');
Squeeze ("..1111111111111111111111111111111111111111111111111111111111111117777888", '7');
Squeeze ("I never give 'em hell, I just tell the truth, and they think it's hell. ", '.');
Squeeze (" --- Harry S Truman ", ' ');
Squeeze (" --- Harry S Truman ", '-');
Squeeze (" --- Harry S Truman ", 'r');
end Test_Squeezable;
</lang>
 
{{out}}
<pre>Character to be squeezed: ' '
Input = <<<>>>, length = 0
Output = <<<>>>, length = 0
Character to be squeezed: '-'
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
Character to be squeezed: '7'
Input = <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length = 72
Output = <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>, length = 69
Character to be squeezed: '.'
Input = <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72
Output = <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72
Character to be squeezed: ' '
Input = <<< --- Harry S Truman >>>, length = 72
Output = <<< --- Harry S Truman >>>, length = 20
Character to be squeezed: '-'
Input = <<< --- Harry S Truman >>>, length = 72
Output = <<< - Harry S Truman >>>, length = 70
Character to be squeezed: 'r'
Input = <<< --- Harry S Truman >>>, length = 72
Output = <<< --- Hary S Truman >>>, length = 71</pre>
 
=={{header|AWK}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.