Determine if a string is squeezable: Difference between revisions

Add Cowgol
(Add BASIC)
(Add Cowgol)
Line 887:
becomes: <<< --- Hary S Truman >>> (len 71)
</pre>
 
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
include "strings.coh";
 
sub squeeze(ch: uint8, str: [uint8], buf: [uint8]): (r: [uint8]) is
r := buf;
var prev: uint8 := 0;
while [str] != 0 loop
if prev != ch or [str] != ch then
prev := [str];
[buf] := prev;
buf := @next buf;
end if;
str := @next str;
end loop;
[buf] := 0;
end sub;
 
sub squeezeAndPrint(ch: uint8, str: [uint8]) is
sub bracketLength(str: [uint8]) is
print_i32(StrLen(str) as uint32);
print(" <<<");
print(str);
print(">>>\n");
end sub;
var buf: uint8[256];
bracketLength(str);
bracketLength(squeeze(ch, str, &buf[0]));
print_nl();
end sub;
 
var strs: [uint8][] := {
"",
"\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ",
"..1111111111111111111111111111111111111111111111111111111111111117777888",
"I never give 'em hell, I just tell the truth, and they think it's hell. ",
" --- Harry S Truman "
};
 
squeezeAndPrint(' ', strs[0]);
squeezeAndPrint('-', strs[1]);
squeezeAndPrint('7', strs[2]);
squeezeAndPrint('.', strs[3]);
squeezeAndPrint(' ', strs[4]);
squeezeAndPrint('-', strs[4]);
squeezeAndPrint('r', strs[4]);</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>>>
69 <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>
 
72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
 
72 <<< --- Harry S Truman >>>
20 <<< --- Harry S Truman >>>
 
72 <<< --- Harry S Truman >>>
70 <<< - Harry S Truman >>>
 
72 <<< --- Harry S Truman >>>
71 <<< --- Hary S Truman >>></pre>
 
=={{header|D}}==
Line 950 ⟶ 1,019:
old: <<< --- Harry S Truman >>>, length = 72
new: <<< --- Hary S Truman >>>, length = 71</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
2,115

edits