Determine if a string is squeezable: Difference between revisions

Line 558:
result: <<< --- Hary S Truman >>>, length: 71
</pre>
 
=={{header|D}}==
{{trans|C#}}
<lang d>import std.stdio;
 
void squeezable(string s, char rune) {
writeln("squeeze: '", rune, "'");
writeln("old: <<<", s, ">>>, length = ", s.length);
 
write("new: <<<");
char last = '\0';
int len = 0;
foreach (c; s) {
if (c != last || c != rune) {
write(c);
len++;
}
last = c;
}
writeln(">>>, length = ", len);
 
writeln;
}
 
void main() {
squeezable(``, ' ');
squeezable(`"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln `, '-');
squeezable(`..1111111111111111111111111111111111111111111111111111111111111117777888`, '7');
squeezable(`I never give 'em hell, I just tell the truth, and they think it's hell. `, '.');
 
string s = ` --- Harry S Truman `;
squeezable(s, ' ');
squeezable(s, '-');
squeezable(s, 'r');
}</lang>
{{out}}
<pre>squeeze: ' '
old: <<<>>>, length = 0
new: <<<>>>, length = 0
 
squeeze: '-'
old: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>, length = 72
new: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>, length = 70
 
squeeze: '7'
old: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length = 72
new: <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>, length = 69
 
squeeze: '.'
old: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72
new: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72
 
squeeze: ' '
old: <<< --- Harry S Truman >>>, length = 72
new: <<< --- Harry S Truman >>>, length = 20
 
squeeze: '-'
old: <<< --- Harry S Truman >>>, length = 72
new: <<< - Harry S Truman >>>, length = 70
 
squeeze: 'r'
old: <<< --- Harry S Truman >>>, length = 72
new: <<< --- Hary S Truman >>>, length = 71</pre>
 
=={{header|Factor}}==
1,452

edits