Determine if a string is squeezable: Difference between revisions

Add BCPL
No edit summary
(Add BCPL)
Line 597:
71<<< --- Hary S Truman >>></pre>
 
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
 
// Squeeze a string
let squeeze(in, ch, out) = valof
$( out%0 := 0
for i=1 to in%0
if i=1 | in%i~=ch | in%(i-1)~=ch
$( out%0 := out%0 + 1
out%(out%0) := in%i
$)
resultis out
$)
 
// Print string with brackets and length
let brackets(s) be
writef("%N: <<<%S>>>*N", s%0, s)
 
// Print original and collapsed version
let show(s, ch) be
$( let v = vec 1+255/BYTESPERWORD
writef("Character: '%C'*N", ch)
brackets(s)
brackets(squeeze(s, ch, v))
wrch('*N')
$)
 
let start() be
$( let s1=""
let s2="*"If I were two-faced, would I be wearing this one?*" --- Abraham Lincoln "
let s3="..1111111111111111111111111111111111111111111111111111111111111117777888"
let s4="I never give 'em hell, I just tell the truth, and they think it's hell. "
let s5=" --- Harry S Truman "
 
show(s1, ' ')
show(s2, '-')
show(s3, '7')
show(s4, '.')
show(s5, ' ')
show(s5, '-')
show(s5, 'r')
$)</lang>
{{out}}
<pre>Character: ' '
0: <<<>>>
0: <<<>>>
 
Character: '-'
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 >>>
 
Character: '7'
72: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
69: <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>
 
Character: '.'
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. >>>
 
Character: ' '
72: <<< --- Harry S Truman >>>
20: <<< --- Harry S Truman >>>
 
Character: '-'
72: <<< --- Harry S Truman >>>
70: <<< - Harry S Truman >>>
 
Character: 'r'
72: <<< --- Harry S Truman >>>
71: <<< --- Hary S Truman >>></pre>
=={{header|C}}==
Identical implementation as in [[Determine_if_a_string_is_collapsible]], as both tasks are very similar. The Lincoln quote contains backslashes to accommodate the double quotes via the command line. strcmpi is not part of the C Standard Library, thus comment out the definition in the code if testing on a system where it is already included.
2,114

edits