Determine if a string is squeezable: Difference between revisions
Content deleted Content added
Added more examples for the C implementation |
Catskill549 (talk | contribs) |
||
Line 104: | Line 104: | ||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f DETERMINE_IF_A_STRING_IS_SQUEEZABLE.AWK |
|||
BEGIN { |
|||
arr[++n] = "" ; arr2[n] = " " |
|||
arr[++n] = "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " ; arr2[n] = "-" |
|||
arr[++n] = "..1111111111111111111111111111111111111111111111111111111111111117777888" ; arr2[n] = "7" |
|||
arr[++n] = "I never give 'em hell, I just tell the truth, and they think it's hell. " ; arr2[n] = "." |
|||
arr[++n] = " --- Harry S Truman " ; arr2[n] = " -r" |
|||
arr[++n] = "The better the 4-wheel drive, the further you'll be from help when ya get stuck!" ; arr2[n] = "e" |
|||
arr[++n] = "headmistressship" ; arr2[n] = "s" |
|||
for (i=1; i<=n; i++) { |
|||
for (j=1; j<=length(arr2[i]); j++) { |
|||
main(arr[i],substr(arr2[i],j,1)) |
|||
} |
|||
} |
|||
exit(0) |
|||
} |
|||
function main(str,chr, c,i,new_str,prev_c) { |
|||
for (i=1; i<=length(str); i++) { |
|||
c = substr(str,i,1) |
|||
if (!(prev_c == c && c == chr)) { |
|||
prev_c = c |
|||
new_str = new_str c |
|||
} |
|||
} |
|||
printf("use: '%s'\n",chr) |
|||
printf("old: %2d <<<%s>>>\n",length(str),str) |
|||
printf("new: %2d <<<%s>>>\n\n",length(new_str),new_str) |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
use: ' ' |
|||
old: 0 <<<>>> |
|||
new: 0 <<<>>> |
|||
use: '-' |
|||
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 >>> |
|||
use: '7' |
|||
old: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> |
|||
new: 69 <<<..1111111111111111111111111111111111111111111111111111111111111117888>>> |
|||
use: '.' |
|||
old: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> |
|||
new: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> |
|||
use: ' ' |
|||
old: 72 <<< --- Harry S Truman >>> |
|||
new: 20 <<< --- Harry S Truman >>> |
|||
use: '-' |
|||
old: 72 <<< --- Harry S Truman >>> |
|||
new: 70 <<< - Harry S Truman >>> |
|||
use: 'r' |
|||
old: 72 <<< --- Harry S Truman >>> |
|||
new: 71 <<< --- Hary S Truman >>> |
|||
use: 'e' |
|||
old: 80 <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>> |
|||
new: 79 <<<The better the 4-whel drive, the further you'll be from help when ya get stuck!>>> |
|||
use: 's' |
|||
old: 16 <<<headmistressship>>> |
|||
new: 14 <<<headmistreship>>> |
|||
</pre> |
|||
=={{header|C}}== |
=={{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. |
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. |