Determine if a string is collapsible: Difference between revisions
Content deleted Content added
Not a robot (talk | contribs) Add Cowgol |
|||
Line 895: | Line 895: | ||
becomes: <<< - Hary S Truman >>> (len 17) |
becomes: <<< - Hary S Truman >>> (len 17) |
||
</pre> |
</pre> |
||
=={{header|Cowgol}}== |
|||
<lang cowgol>include "cowgol.coh"; |
|||
include "strings.coh"; |
|||
# Collapse the string at in, and store the result in the given buffer |
|||
sub collapse(in: [uint8], out: [uint8]) is |
|||
var ch := [in]; |
|||
in := @next in; |
|||
loop |
|||
if ch == 0 then |
|||
[out] := 0; |
|||
return; |
|||
elseif [in] != ch then |
|||
[out] := ch; |
|||
out := @next out; |
|||
ch := [in]; |
|||
end if; |
|||
in := @next in; |
|||
end loop; |
|||
end sub; |
|||
# Given a string, collapse it and print all required output |
|||
sub show(str: [uint8]) is |
|||
sub bracket_length(str: [uint8]) is |
|||
print_i32(StrLen(str) as uint32); |
|||
print(" <<<"); |
|||
print(str); |
|||
print(">>>"); |
|||
print_nl(); |
|||
end sub; |
|||
var buf: uint8[256]; |
|||
collapse(str, &buf[0]); |
|||
bracket_length(str); |
|||
bracket_length(&buf[0]); |
|||
print_nl(); |
|||
end sub; |
|||
# Strings from the task |
|||
var strings: [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 " |
|||
}; |
|||
# Collapse and print each string |
|||
var i: @indexof strings := 0; |
|||
while i < @sizeof strings loop |
|||
show(strings[i]); |
|||
i := i + 1; |
|||
end loop;</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|D}}== |
=={{header|D}}== |