Determine if a string is collapsible: Difference between revisions
Content added Content deleted
(Added C implementation) |
Catskill549 (talk | contribs) |
||
Line 87: | Line 87: | ||
<br><br> |
<br><br> |
||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f DETERMINE_IF_A_STRING_IS_COLLAPSIBLE.AWK |
|||
BEGIN { |
|||
for (i=1; i<=9; i++) { |
|||
for (j=1; j<=i; j++) { |
|||
arr[0] = arr[0] i |
|||
} |
|||
} |
|||
arr[++n] = "" |
|||
arr[++n] = "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " |
|||
arr[++n] = "..1111111111111111111111111111111111111111111111111111111111111117777888" |
|||
arr[++n] = "I never give 'em hell, I just tell the truth, and they think it's hell. " |
|||
arr[++n] = " --- Harry S Truman " |
|||
arr[++n] = "The better the 4-wheel drive, the further you'll be from help when ya get stuck!" |
|||
arr[++n] = "headmistressship" |
|||
for (i=0; i<=n; i++) { |
|||
main(arr[i]) |
|||
} |
|||
exit(0) |
|||
} |
|||
function main(str, c,i,new_str,prev_c) { |
|||
for (i=1; i<=length(str); i++) { |
|||
c = substr(str,i,1) |
|||
if (prev_c != c) { |
|||
prev_c = c |
|||
new_str = new_str c |
|||
} |
|||
} |
|||
printf("old: %2d <<<%s>>>\n",length(str),str) |
|||
printf("new: %2d <<<%s>>>\n\n",length(new_str),new_str) |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
old: 45 <<<122333444455555666666777777788888888999999999>>> |
|||
new: 9 <<<123456789>>> |
|||
old: 0 <<<>>> |
|||
new: 0 <<<>>> |
|||
old: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> |
|||
new: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> |
|||
old: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> |
|||
new: 4 <<<.178>>> |
|||
old: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> |
|||
new: 69 <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> |
|||
old: 72 <<< --- Harry S Truman >>> |
|||
new: 17 <<< - Hary S Truman >>> |
|||
old: 80 <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>> |
|||
new: 77 <<<The beter the 4-whel drive, the further you'l be from help when ya get stuck!>>> |
|||
old: 16 <<<headmistressship>>> |
|||
new: 14 <<<headmistreship>>> |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Identical implementation as in [[Determine_if_a_string_is_squeezable]], 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. |
Identical implementation as in [[Determine_if_a_string_is_squeezable]], 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. |
||
Line 313: | Line 372: | ||
Length : 16 |
Length : 16 |
||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang factor>USING: formatting io kernel sbufs sequences strings ; |
<lang factor>USING: formatting io kernel sbufs sequences strings ; |