Determine if a string is squeezable: Difference between revisions

Added Easylang
(Added Easylang)
 
(37 intermediate revisions by 21 users not shown)
Line 97:
{{Template:Strings}}
<br><br>
 
=={{header|11l}}==
{{trans|Java}}
 
<syntaxhighlight lang="11l">F squeeze(input, include)
V s = ‘’
L(i) 0 .< input.len
I i == 0 | input[i - 1] != input[i] | (input[i - 1] == input[i] & input[i] != include)
s ‘’= input[i]
R s
 
V testStrings = [
‘’,
‘"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 ’,
‘122333444455555666666777777788888888999999999’,
‘The better the 4-wheel drive, the further you'll be from help when ya get stuck!’,
‘headmistressship’
]
 
V testChar = [
‘ ’,
‘-’,
‘7’,
‘.’,
‘ -r’,
‘5’,
‘e’,
‘s’
]
 
L(testNum) 0 .< testStrings.len
V s = testStrings[testNum]
L(c) testChar[testNum]
V result = squeeze(s, c)
print("use: '#.'\nold: #2 <<<#.>>>\nnew: #2 <<<#.>>>\n".format(c, s.len, s, result.len, result))</syntaxhighlight>
 
{{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: '5'
old: 45 <<<122333444455555666666777777788888888999999999>>>
new: 41 <<<12233344445666666777777788888888999999999>>>
 
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|8080 Assembly}}==
<syntaxhighlight lang="8080asm">puts: equ 9
org 100h
jmp demo
;;; Squeeze the string at DE on the character in C.
;;; The result is written starting at HL.
squeez: mvi b,'$' ; Last character seen
dcx d ; Move pointer back one item
sqzlp: mvi m,'$' ; Stop on end of string
inx d ; Increment input pointer
ldax d ; Grab character from input string
cmp m ; End reached?
rz ; Then stop
cmp c ; Was it equal to the character to squeeze?
jnz sqzwr ; If not, then write it to the output
mov a,b ; If so, is the previous character?
cmp c
jz sqzlp ; If so, ignore this one
sqzwr: ldax d ; Retrieve the character again
mov m,a ; It goes in the output
mov b,a ; And it is the last character seen
inx h ; Increment output pointer
jmp sqzlp
;;; Print the string in DE and character in C as specified,
;;; squeeze the string and print the output.
prsqz: push b ; Save input parameters
push d
mov a,c ; Store character
sta chval
lxi d,chstr ; Print character
mvi c,puts
call 5
pop h ; Retrieve input string pointer
push h
call prbrkt ; Print the string in brackets
pop d ; Retrieve both input parameters
pop b
lxi h,buffer
call squeez ; Squeeze the input string
lxi h,buffer ; ... fall through to print the result
;;; Write the string at HL and its length in brackets
prbrkt: push h ; Keep the pointer
mvi b,0FFh ; Find the length
mvi a,'$' ; End marker
dcx h
lscan: inr b ; Scan through the string incrementing B
inx h ; and HL until the end is found
cmp m
jnz lscan
mov a,b ; Find high and low digit (assuming < 100)
mvi b,'0'-1
pdigit: inr b ; B = high digit
sui 10
jnc pdigit
lxi h,bstart+1
adi '0'+10
mov m,a ; Write low digit
dcx h
mov m,b ; Write high digit
xchg
mvi c,puts ; Print length and brackets
call 5
pop d ; Retrieve the string pointer
mvi c,puts ; Print the string
call 5
lxi d,bend ; Print the ending brackets
mvi c,puts
jmp 5
;;; Squeeze each string by the given character
demo: lxi h,list ; Pointer to start of list
loop: mov e,m ; Load pointer and character
inx h
mov d,m
inx h
mov c,m
inx h
xra a ; Stop when zero reached
ora c
rz
push h ; Keep list pointer
call prsqz ; Squeeze and print
pop h ; Restore list pointer
jmp loop
;;; Formatting strings
chstr: db 'Character: "'
chval: db '*"',13,10,'$'
bstart: db '##<<<$'
bend: db '>>>'
nl: db 13,10,'$'
;;; Input strings
str1: db '$'
str2: db '"If I were two-faced, would I be wearing'
db ' this one?" --- Abraham Lincoln $'
str3: db '..11111111111111111111111111111111111111'
db '11111111111111111111111117777888$'
str4: db 'I never give ',39,'em hell, I just tell the t'
db 'ruth, and they think it',39,'s hell. $'
str5: db ' '
db ' --- Harry S Truman $'
;;; Pairs of string pointers and characters to squeeze
list: dw str1
db ' '
dw str2
db '-'
dw str3
db '7'
dw str4
db '.'
dw str5
db ' '
dw str5
db '-'
dw str5
db 'r'
dw 0
db 0
buffer: equ $</syntaxhighlight>
{{out}}
<pre>Character: " "
00<<<>>>
00<<<>>>
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|Action!}}==
<syntaxhighlight lang="action!">PROC Squeeze(CHAR ARRAY in,out CHAR a)
BYTE i,j
CHAR c
 
j=1 c=0
FOR i=1 TO in(0)
DO
IF in(i)#c OR in(i)#a THEN
c=in(i)
out(j)=c
j==+1
FI
OD
out(0)=j-1
RETURN
 
PROC Test(CHAR ARRAY s CHAR a)
CHAR ARRAY c(100)
BYTE CH=$02FC ;Internal hardware value for last key pressed
 
Squeeze(s,c,a)
PrintF("Character to squeeze: ""%C""%E",a)
PrintF("<<<%S>>> (len=%B)%E",s,s(0))
PrintF("<<<%S>>> (len=%B)%E",c,c(0))
PutE()
PrintE("Press any key to continue")
PutE()
 
DO UNTIL CH#$FF OD
CH=$FF
RETURN
 
PROC Main()
Test("",' )
Test("""If I were two-faced, would I be wearing this one?"" --- Abraham Lincoln ",'-)
Test("..1111111111111111111111111111111111111111111111111111111111111117777888",'7)
Test("I never give 'em hell, I just tell the truth, and they think it's hell. ",'.)
Test(" --- Harry S Truman ",' )
Test(" --- Harry S Truman ",'-)
Test(" --- Harry S Truman ",'r)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Determine_if_a_string_is_squeezable.png Screenshot from Atari 8-bit computer]
<pre>
Character to squeeze: " "
<<<>>> (len=0)
<<<>>> (len=0)
 
Character to squeeze: "-"
<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> (len=72)
<<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> (len=70)
 
Character to squeeze: "7"
<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (len=72)
<<<..1111111111111111111111111111111111111111111111111111111111111117888>>> (len=69)
 
Character to squeeze: "."
<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (len=72)
<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (len=72)
 
Character to squeeze: " "
<<< --- Harry S Truman >>> (len=72)
<<< --- Harry S Truman >>> (len=20)
 
Character to squeeze: "-"
<<< --- Harry S Truman >>> (len=72)
<<< - Harry S Truman >>> (len=70)
 
Character to squeeze: "r"
<<< --- Harry S Truman >>> (len=72)
<<< --- Hary S Truman >>> (len=71)
</pre>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Squeezable is
procedure Squeeze (S : in String; C : in Character) is
Line 124 ⟶ 420:
Squeeze (" --- Harry S Truman ", 'r');
end Test_Squeezable;
</syntaxhighlight>
</lang>
 
{{out}}
Line 150 ⟶ 446:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN
# returns a squeezed version of s #
# i.e. s with adjacent duplicate c characters removed #
Line 187 ⟶ 483:
test squeeze( hst, "-" );
test squeeze( hst, "r" )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 205 ⟶ 501:
-> <<< --- Hary S Truman >>> (length 71)
</pre>
 
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">task←{
⍝ Squeeze a string
squeeze ← ⊢(/⍨)≠∨1,1↓⊣≠¯1⌽⊢
 
⍝ Display string and length in the manner given in the task
display ← {(¯2↑⍕≢⍵),' «««',⍵,'»»»'}
 
⍝ Squeeze string and display output
show ← {
r← ⊂'chr: ''',⍺,''''
r←r,⊂' in: ',display ⍵
r←r,⊂'out: ',display ⍺ squeeze ⍵
↑r,⊂''
}
 
⍝ Strings from the task
s1←''
s2←'"If I were two-faced, would I be wearing this one?"'
s2←s2,' --- Abraham Lincoln '
s3←'..1111111111111111111111111111111111111111111111111'
s3←s3,'111111111111117777888'
s4←'I never give ''em hell, I just tell the truth, and t'
s4←s4,'hey think it''s hell. '
s5←' '
s5←s5,' --- Harry S Truman '
 
⎕←' ' show s1
⎕←'-' show s2
⎕←'7' show s3
⎕←'.' show s4
{⎕←⍵ show s5}¨' -r' ⍝⍝ note use of 'show' here in a lambda (dfn)
}</syntaxhighlight>
{{works with|GNU APL}}
<syntaxhighlight lang="apl">
#!/usr/local/bin/apl --script
 
∇r ← c squeeze s
s ← 0,s,0
r ← 1↓¯2↓∊((s≠c)⊂s),¨c
 
∇r ← display s
r ← (¯2↑⍕≢s),' «««',s,'»»»'
 
∇r ← c show s
r ← ⊂ ("chr: '",c,"'")
r ← r, ⊂(" in: ",display s)
r ← r, ⊂ ("out: ",display c squeeze s)
r ← ⊃r
 
∇main
s1←''
s2←'"If I were two-faced, would I be wearing this one?"'
s2←s2,' --- Abraham Lincoln '
s3←'..1111111111111111111111111111111111111111111111111'
s3←s3,'111111111111117777888'
s4←'I never give ''em hell, I just tell the truth, and t'
s4←s4,'hey think it''s hell. '
s5←' '
s5←s5,' --- Harry S Truman '
 
⎕←' ' show s1
⎕←'-' show s2
⎕←'7' show s3
⎕←'.' show s4
 
⊃ {⍵ show s5}¨' -r'
</syntaxhighlight>
 
{{out}}
<pre>chr: ' '
in: 0 «««»»»
out: 0 «««»»»
chr: '-'
in: 72 «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
out: 70 «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
chr: '7'
in: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
out: 69 «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
chr: '.'
in: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
out: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
chr: ' '
in: 72 ««« --- Harry S Truman »»»
out: 20 ««« --- Harry S Truman »»»
chr: '-'
in: 72 ««« --- Harry S Truman »»»
out: 70 ««« - Harry S Truman »»»
chr: 'r'
in: 72 ««« --- Harry S Truman »»»
out: 71 ««« --- Hary S Truman »»»</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">squeezable_string(str, char){
for i, ch in StrSplit(str){
if (ch <> prev) || !InStr(ch, char)
Line 220 ⟶ 619:
)"
return result
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">data := [""
, """If I were two-faced, would I be wearing this one?"" --- Abraham Lincoln "
, "..1111111111111111111111111111111111111111111111111111111111111117777888"
Line 232 ⟶ 631:
for i, char in [" ","-","r"]
MsgBox % squeezable_string(str, char)
return</langsyntaxhighlight>
Outputs:<pre>---------------------------
Original string: 0 characters «««»»»
Line 264 ⟶ 663:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f DETERMINE_IF_A_STRING_IS_SQUEEZABLE.AWK
BEGIN {
Line 293 ⟶ 692:
printf("new: %2d <<<%s>>>\n\n",length(new_str),new_str)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 333 ⟶ 732:
</pre>
 
=={{header|BASIC}}==
<syntaxhighlight lang="basic">10 DEFINT A-Z
20 READ N: DIM S$(N): FOR I=1 TO N: READ S$(I): NEXT
30 READ S,C$: IF S=0 THEN END
40 PRINT "Character: '";C$;"'"
50 O$=S$(S): GOSUB 200
60 I$=S$(S): GOSUB 100: GOSUB 200
70 PRINT
80 GOTO 30
100 REM --
101 REM -- Squeeze I$ on character C$, output in O$
102 REM --
105 O$ = ""
110 X = INSTR(I$,C$)
120 IF X = 0 THEN O$ = O$ + I$: RETURN
130 O$ = O$ + LEFT$(I$,X)
140 FOR X=X TO LEN(I$): IF MID$(I$,X,1) = C$ THEN NEXT
150 I$ = RIGHT$(I$,LEN(I$)-X+1)
160 GOTO 110
200 REM --
201 REM -- Display O$ and its length in brackets
202 REM --
210 PRINT USING "##";LEN(O$);
220 PRINT "<<<";O$;">>>"
230 RETURN
400 REM -- Strings
410 DATA 5
415 DATA""
420 DATA"'If I were two-faced, would I be wearing this one?' --- Abraham Lincoln "
430 DATA"..1111111111111111111111111111111111111111111111111111111111111117777888"
440 DATA"I never give 'em hell, I just tell the truth, and they think it's hell. "
450 DATA" --- Harry S Truman "
500 REM -- String index and character to squeeze
510 DATA 1," ", 2,"-", 3,"7", 4,".", 5," ", 5,"-", 5,"r", 0,""</syntaxhighlight>
{{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|BCPL}}==
<syntaxhighlight 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')
$)</syntaxhighlight>
{{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.
<syntaxhighlight lang="c">
<lang C>
#include<string.h>
#include<stdlib.h>
Line 506 ⟶ 1,038:
return 0;
}
</syntaxhighlight>
</lang>
Output :
<pre>
Line 598 ⟶ 1,130:
</pre>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using static System.Linq.Enumerable;
 
Line 625 ⟶ 1,157:
static string Squeeze(string s, char c) => string.IsNullOrEmpty(s) ? "" :
s[0] + new string(Range(1, s.Length - 1).Where(i => s[i] != c || s[i] != s[i - 1]).Select(i => s[i]).ToArray());
}</langsyntaxhighlight>
{{out}}
<pre>
Line 653 ⟶ 1,185:
=={{header|C++}}==
The solution is a straightforward application of the standard library function "unique".
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <string>
#include <iostream>
Line 683 ⟶ 1,215:
test(truman, 'r');
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 717 ⟶ 1,249:
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
<lang Clojure>
(defn squeeze [s c]
(let [spans (partition-by #(= c %) s)
Line 730 ⟶ 1,262:
(println (format "Input: <<<%s>>> (len %d)\n" s (count s))
(format "becomes: <<<%s>>> (len %d)" out (count out)))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 762 ⟶ 1,294:
becomes: <<< --- Hary S Truman >>> (len 71)
</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
include "strings.coh";
 
sub squeeze(ch: uint8, str: [uint8], buf: [uint8]): (r: [uint8]) is
r := buf;
var prev: uint8 := 0;
while [str] != 0 loop
if prev != ch or [str] != ch then
prev := [str];
[buf] := prev;
buf := @next buf;
end if;
str := @next str;
end loop;
[buf] := 0;
end sub;
 
sub squeezeAndPrint(ch: uint8, str: [uint8]) is
sub bracketLength(str: [uint8]) is
print_i32(StrLen(str) as uint32);
print(" <<<");
print(str);
print(">>>\n");
end sub;
var buf: uint8[256];
bracketLength(str);
bracketLength(squeeze(ch, str, &buf[0]));
print_nl();
end sub;
 
var strs: [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 "
};
 
squeezeAndPrint(' ', strs[0]);
squeezeAndPrint('-', strs[1]);
squeezeAndPrint('7', strs[2]);
squeezeAndPrint('.', strs[3]);
squeezeAndPrint(' ', strs[4]);
squeezeAndPrint('-', strs[4]);
squeezeAndPrint('r', strs[4]);</syntaxhighlight>
{{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>>>
69 <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>
 
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. >>>
 
72 <<< --- Harry S Truman >>>
20 <<< --- Harry S Truman >>>
 
72 <<< --- Harry S Truman >>>
70 <<< - Harry S Truman >>>
 
72 <<< --- Harry S Truman >>>
71 <<< --- Hary S Truman >>></pre>
 
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.stdio;
 
void squeezable(string s, char rune) {
Line 796 ⟶ 1,397:
squeezable(s, '-');
squeezable(s, 'r');
}</langsyntaxhighlight>
{{out}}
<pre>squeeze: ' '
Line 825 ⟶ 1,426:
old: <<< --- Harry S Truman >>>, length = 72
new: <<< --- Hary S Truman >>>, length = 71</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|Java}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Determine_if_a_string_is_squeezable;
 
Line 853 ⟶ 1,455:
begin
if (i = 1) or (s[i - 1] <> s[i]) or ((s[i - 1] = s[i]) and (s[i] <> include)) then
sb.Append(s[i]);
end;
Result := sb.ToString;
Line 871 ⟶ 1,473:
end;
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>use: " "
Line 912 ⟶ 1,514:
old: 16 <<<headmistressship>>>
new: 14 <<<headmistreship>>></pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
func$ squeeze s$ x$ .
for c$ in strchars s$
if c$ <> x$ or c$ <> cc$
r$ &= c$
.
cc$ = c$
.
return r$
.
proc do s$ x$ . .
print "'" & x$ & "'"
print "«««" & s$ & "»»» (" & len s$ & ")"
r$ = squeeze s$ x$
print "«««" & r$ & "»»» (" & len r$ & ")"
print ""
.
do "" " "
do "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " "-"
do "..1111111111111111111111111111111111111111111111111111111111111117777888" "7"
do "I never give 'em hell, I just tell the truth, and they think it's hell. " "."
do " --- Harry S Truman " " "
do " --- Harry S Truman " "-"
do " --- Harry S Truman " "r"
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Determine if a string is squeezable. Nigel Galloway: June 9th., 2020
let squeeze n i=if String.length n=0 then None else
Line 929 ⟶ 1,558:
isSqueezable "I never give 'em hell, I just tell the truth, and they think it's hell. " '.'
let fN=isSqueezable " --- Harry S Truman " in fN ' '; fN '-'; fN 'r'
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 940 ⟶ 1,569:
'r' squeezes <<< --- Harry S Truman >>> (length 72) to <<< --- Hary S Truman >>> (length 71)
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting fry io kernel math sbufs sequences strings ;
IN: rosetta-code.squeeze
 
Line 971 ⟶ 1,601:
[ show-squeeze ] 2tri@ ;
MAIN: squeeze-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 1,001 ⟶ 1,631:
Before squeeze: ««« --- Harry S Truman »»» (length 72)
After squeeze: ««« --- Hary S Truman »»» (length 71)
</pre>
 
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
program main
implicit none
character(len=:),allocatable :: strings(:)
 
strings=[ character(len=72) :: &
'', &
'"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' &
]
 
call printme( trim(strings(1)), ' ' )
call printme( strings(2:4), ['-','7','.'] )
call printme( strings(5), [' ','-','r'] )
 
contains
 
impure elemental subroutine printme(str,chr)
character(len=*),intent(in) :: str
character(len=1),intent(in) :: chr
character(len=:),allocatable :: answer
write(*,'(a)')repeat('=',9)
write(*,'("IN: <<<",g0,">>>")')str
answer=compact(str,chr)
write(*,'("OUT: <<<",g0,">>>")')answer
write(*,'("LENS: ",*(g0,1x))')"from",len(str),"to",len(answer),"for a change of",len(str)-len(answer)
write(*,'("CHAR: ",g0)')chr
end subroutine printme
 
elemental function compact(str,charp) result (outstr)
 
character(len=*),intent(in) :: str
character(len=1),intent(in) :: charp
character(len=:),allocatable :: outstr
character(len=1) :: ch, last_one
integer :: i, pio ! position in output
 
outstr=repeat(' ',len(str)) ! start with a string big enough to hold any output
if(len(outstr)==0)return ! handle edge condition
last_one=str(1:1) ! since at least this long start output with first character
outstr(1:1)=last_one
pio=1
 
do i=2,len(str)
ch=str(i:i)
pio=pio+merge(0,1, ch.eq.last_one.and.ch.eq.charp) ! decide whether to advance before saving
outstr(pio:pio)=ch ! store new one or overlay the duplcation
last_one=ch
enddo
 
outstr=outstr(:pio) ! trim the output string to just what was set
end function compact
 
end program main
}</syntaxhighlight>
{{out}}
<pre>
=========
IN: <<<>>>
OUT: <<<>>>
LENS: from 0 to 0 for a change of 0
CHAR:
=========
IN: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
OUT: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>
LENS: from 72 to 70 for a change of 2
CHAR: -
=========
IN: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
OUT: <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>
LENS: from 72 to 69 for a change of 3
CHAR: 7
=========
IN: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
OUT: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
LENS: from 72 to 72 for a change of 0
CHAR: .
=========
IN: <<< --- Harry S Truman >>>
OUT: <<< --- Harry S Truman >>>
LENS: from 72 to 20 for a change of 52
CHAR:
=========
IN: <<< --- Harry S Truman >>>
OUT: <<< - Harry S Truman >>>
LENS: from 72 to 70 for a change of 2
CHAR: -
=========
IN: <<< --- Harry S Truman >>>
OUT: <<< --- Hary S Truman >>>
LENS: from 72 to 71 for a change of 1
CHAR: r
</pre>
 
 
 
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
function squeeze(byval s as string,target as string) as string
dim as string g
dim as long n
for n =0 to len(s)-2
if s[n]=asc(target) then
if s[n]<>s[n+1] then g+=chr(s[n])
else
g+=chr(s[n])
end if
next n
if len(s) then g+=chr(s[n])
return g
end function
 
dim as string z,o
print "character "" """
o=""
print "original ";o;tab(90);"(";len(o);")"
z=Squeeze("", " ")
print "squeeze "; z;tab(90);"(";len(z);")"
print
print "character ""-"""
o= """If I were two-faced, would I be wearing this one?"" --- Abraham Lincoln "
print "original ";o;tab(90);"(";len(o);")"
z=Squeeze(o,"-")
print "squeeze "; z;tab(90);"(";len(z);")"
print
print "character ""7"""
o="..1111111111111111111111111111111111111111111111111111111111111117777888"
print "original ";o;tab(90);"(";len(o);")"
z=Squeeze(o,"7")
print "squeeze "; z;tab(90);"(";len(z);")"
print
print "character ""."""
o="I never give 'em hell, I just tell the truth, and they think it's hell. "
print "original ";o;tab(90);"(";len(o);")"
z=Squeeze(o,".")
print "squeeze ";z ;tab(90);"(";len(z);")"
print
dim as string s = " --- Harry S Truman "
print "character "" """
o=" --- Harry S Truman "
print "original ";o;tab(90);"(";len(o);")"
z=Squeeze(s, " ")
print "squeeze ";z ;tab(90);"(";len(z);")"
print
print "character ""-"""
o=" --- Harry S Truman "
print "original ";o;tab(90);"(";len(o);")"
z=Squeeze(s, "-")
print "squeeze "; z;tab(90);"(";len(z);")"
print
print "character ""r"""
o=" --- Harry S Truman "
print "original ";o;tab(90);"(";len(o);")"
z=Squeeze(s, "r")
print "squeeze "; z;tab(90);"(";len(z);")"
sleep
</syntaxhighlight>
{{out}}
<pre>
character " "
original ( 0)
squeeze ( 0)
 
character "-"
original "If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ( 72)
squeeze "If I were two-faced, would I be wearing this one?" - Abraham Lincoln ( 70)
 
character "7"
original ..1111111111111111111111111111111111111111111111111111111111111117777888 ( 72)
squeeze ..1111111111111111111111111111111111111111111111111111111111111117888 ( 69)
 
character "."
original I never give 'em hell, I just tell the truth, and they think it's hell. ( 72)
squeeze I never give 'em hell, I just tell the truth, and they think it's hell. ( 72)
 
character " "
original --- Harry S Truman ( 72)
squeeze --- Harry S Truman ( 20)
 
character "-"
original --- Harry S Truman ( 72)
squeeze - Harry S Truman ( 70)
 
character "r"
original --- Harry S Truman ( 72)
squeeze --- Hary S Truman ( 71)
</pre>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">squeeze[str, ch] :=
{
println["Use: '$ch'"]
println["old: " + length[str] + " <<<$str>>>"]
str =~ subst["($ch)\\1+", "$$1", "g"]
println["new: " + length[str] + " <<<$str>>>"]
}
 
lines = [["", [""]],
[""""If I were two-faced, would I be wearing this one?" --- Abraham Lincoln """, ["-"]],
["..1111111111111111111111111111111111111111111111111111111111111117777888", ["7"]],
["I never give 'em hell, I just tell the truth, and they think it's hell. ", ["."]],
[" --- Harry S Truman ",[" ", "-", "r"]]]
 
for [line, chars] = lines
for char = chars
println[squeeze[line, char]]</syntaxhighlight>
{{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: 69 <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>
 
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 >>>
 
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,048 ⟶ 1,920:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,098 ⟶ 1,970:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class StringSqueezable {
static void main(String[] args) {
String[] testStrings = [
Line 1,130 ⟶ 2,002:
return sb.toString()
}
}</langsyntaxhighlight>
{{out}}
<pre>use: ' '
Line 1,173 ⟶ 2,045:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
 
input :: [(String, Char)]
Line 1,198 ⟶ 2,070:
main =
mapM_ (\(a, b, c) -> printf "squeeze: '%c'\nold: %3d «««%s»»»\nnew: %3d «««%s»»»\n\n" c (length a) a (length b) b)
$ (\(s, c) -> (s, collapse s c, c)) <$> input</langsyntaxhighlight>
{{out}}
<pre>
Line 1,240 ⟶ 2,112:
=={{header|J}}==
Invoking j with the standard startup scripts includes definitions to delete excess, leading, and trailing blanks. <tt>deb</tt> corresponds to task squeeze, additionally removing leading and trailing blanks. The definitions are
<syntaxhighlight lang="j">
<lang j>
dlb
}.~ (=&' ' (i.) 0:)
Line 1,247 ⟶ 2,119:
deb
#~ ((+.) (1: |. (> </\)))@(' '&~:)
</syntaxhighlight>
</lang>
Onward with the task. <tt>squeeze</tt> makes a logical vector of inequality and uses Boolean algebra against itself shifted by one position to mask the text.
<pre>
Line 1,269 ⟶ 2,141:
)
</pre>
<syntaxhighlight lang="j">
<lang J>
'x' squeeze 'xabxxcxdxxx' NB. example
xabxcxdx
Line 1,304 ⟶ 2,176:
│<<< --- Hary S Truman>>> │
└──────────────────────────────────────────────────────────────────────────────┘
</syntaxhighlight>
</lang>
=== APL translation ===
{{trans|APL}}
<syntaxhighlight lang="j">NB. Note |.!.1 here instead of the APL version's 1 , }. to more elegantly handle the null case in J
sq =: ] #~ ~: +. _1 |.!.1 ] ~: 1 |. ]</syntaxhighlight>
 
'''Run''':
<syntaxhighlight lang="j">require'format/printf' NB. For formatting the output
'C I' =: <"1 |: P =: ; ;"0 1^:(0<#@[)&.>/"1 (({.~ ; (}.~>:)) i.&'|')&> LF cut noun define
|
-|"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln
7|..1111111111111111111111111111111111111111111111111111111111111117777888
.|I never give 'em hell, I just tell the truth, and they think it's hell.
-r| --- Harry S Truman
)
S =: sq&.>/"1 P
smoutput 'chr: ''%s''\nin: %d «««%s»»»\nout: %d «««%s»»»\n' sprintf C ,. (#&.>I),.I ,. (#&.>S),.S</syntaxhighlight>
{{out}}
<pre>chr: ''
in: 0 «««»»»
out: 0 «««»»»
chr: '-'
in: 72 «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
out: 70 «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
chr: '7'
in: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
out: 69 «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
chr: '.'
in: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
out: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
 
chr: ' '
in: 72 ««« --- Harry S Truman »»»
out: 20 ««« --- Harry S Truman »»»
chr: '-'
in: 72 ««« --- Harry S Truman »»»
out: 70 ««« - Harry S Truman »»»
chr: 'r'
in: 72 ««« --- Harry S Truman »»»
out: 71 ««« --- Hary S Truman »»»</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
<lang Java>
 
// Title: Determine if a string is squeezable
Line 1,352 ⟶ 2,269:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,404 ⟶ 2,321:
 
* "SQUEEZE returns a string that reduces all multiple occurrences of a [specified] character to one ..."
* The example given is: 'in abceffect 'SabcSS:SSdefSSSghSS' => 'SabcS:SdefSghS' where defS signifies a ghsingle space 'character.
** i.e. 'SabcSS:SSdefSSSghSS' where S signifies a single space character.
 
=== jq program ===
<langsyntaxhighlight lang="jq"># Assume $c is specified as a single character correctly
def squeeze($c): gsub("[\($c)]+"; $c);
 
Line 1,417 ⟶ 2,333:
(s | "Original: \(Guillemets) has length \(length)",
(squeeze($c) | "result is \(Guillemets) with length \(length)")),
"";</langsyntaxhighlight>
=== Demo ===
<langsyntaxhighlight lang="jq"> Squeeze("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "; "-"),
Squeeze ("..1111111111111111111111111111111111111111111111111111111111111117777888"; "7"),
Squeeze ("I never give 'em hell, I just tell the truth, and they think it's hell. "; "."),
Line 1,426 ⟶ 2,342:
Squeeze (" --- Harry S Truman "; "r"),
 
Squeeze("SabcSS:SSdefSSSghSS";"S")</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">Squeeze character: -
Original: «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» has length 72
result is «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» with length 70
Line 1,454 ⟶ 2,370:
Squeeze character: S
Original: «««SabcSS:SSdefSSSghSS»»» has length 19
result is «««SabcS:SdefSghS»»» with length 14</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">const teststringpairs = [
("", ' '),
(""""If I were two-faced, would I be wearing this one?" --- Abraham Lincoln """, '-'),
Line 1,482 ⟶ 2,398:
":\n«««$t»»» (length $(length(t))).\n")
end
</langsyntaxhighlight>{{out}}
<pre>
«««»»» (length 0)
Line 1,515 ⟶ 2,431:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">fun main() {
val testStrings = arrayOf(
"",
Line 1,551 ⟶ 2,467:
}
return sb.toString()
}</langsyntaxhighlight>
{{out}}
<pre>use: ' '
Line 1,595 ⟶ 2,511:
=={{header|Lua}}==
{{trans|D}}
<langsyntaxhighlight lang="lua">function squeezable(s, rune)
print("squeeze: `" .. rune .. "`")
print("old: <<<" .. s .. ">>>, length = " .. string.len(s))
Line 1,626 ⟶ 2,542:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>squeeze: ` `
Line 1,655 ⟶ 2,571:
old: <<< --- Harry S Truman >>>, length = 72
new: <<< --- Hary S Truman >>>, length = 71</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[Squeeze]
Squeeze[s_String,sq_String]:=StringReplace[s,x:(sq..):>sq]
s={
"",
"\"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 "
};
Squeeze[s[[1]],""]
Squeeze[s[[2]],"-"]
Squeeze[s[[3]],"7"]
Squeeze[s[[4]],"."]
Squeeze[s[[5]]," "]
Squeeze[s[[5]],"-"]
Squeeze[s[[5]],"r"]</syntaxhighlight>
{{out}}
<pre>""
"\"If I were two-faced, would I be wearing this one?\" - Abraham Lincoln "
"..1111111111111111111111111111111111111111111111111111111111111117888"
"I never give 'em hell, I just tell the truth, and they think it's hell. "
" --- Harry S Truman "
" - Harry S Truman "
" --- Hary S Truman "</pre>
 
 
Line 1,661 ⟶ 2,603:
squeeze is a base function Matlab and Octave, therefore its name is changed to squeezee()
 
<syntaxhighlight lang="matlab">
<lang Matlab>
function r = squeezee(s,c)
ix = [];
Line 1,688 ⟶ 2,630:
squeezee('║ --- Harry S Truman ║', 'r')
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,734 ⟶ 2,676:
ans = ║ --- Hary S Truman ║
</pre>
 
=={{header|NetLogo}}==
 
1. Paste this code into the Code Pane
2. In the Interface Pane, in the Command Center, type "demo" and hit enter.
 
<syntaxhighlight lang="netlogo">
to-report split [ string ]
;; utility reporter to split a string into a list
report n-values length string [ [ n ] -> item n string ]
end
 
to-report squeeze [ character string ]
;; reporter that actually does the squeeze function
;; remote immeadiately repeating instances of character from string
ifelse ( string = "" )
[ report "" ] ;; empty input, report empty output
[ report
reduce
[ [ a b ] ->
( word a
ifelse-value b = character and b = last a
[ "" ]
[ b ]
)
]
split string
]
end
 
to-report bracket [ string ]
;; utility reporter to enclose a string in brackets
report ( word "<<<" string ">>>" )
end
 
to-report format [ string ]
;; utility reporter to format the output as required
report ( word bracket string " " length string )
end
 
to demo-squeeze [ character string ]
;; procedure to display the required output
output-print bracket character
output-print format string
output-print format squeeze character string
end
 
to demo
;; procedure to perform the test cases
demo-squeeze " " ""
demo-squeeze "-" "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "
demo-squeeze "7" "..1111111111111111111111111111111111111111111111111111111111111117777888"
demo-squeeze "." "I never give 'em hell, I just tell the truth, and they think it's hell. "
demo-squeeze " " " --- Harry S Truman "
demo-squeeze "-" " --- Harry S Truman "
demo-squeeze "r" " --- Harry S Truman "
end
</syntaxhighlight>
 
{{out}}
 
<pre>observer> demo
<<< >>>
<<<>>> 0
<<<>>> 0
<<<->>>
<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> 72
<<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> 70
<<<7>>>
<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> 72
<<<..1111111111111111111111111111111111111111111111111111111111111117888>>> 69
<<<.>>>
<<<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. >>> 72
<<< >>>
<<< --- Harry S Truman >>> 72
<<< --- Harry S Truman >>> 20
<<<->>>
<<< --- Harry S Truman >>> 72
<<< - Harry S Truman >>> 70
<<<r>>>
<<< --- Harry S Truman >>> 72
<<< --- Hary S Truman >>> 71</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import unicode, strformat
 
proc squeeze(s: string; ch: Rune) =
Line 1,771 ⟶ 2,796:
for i, s in Strings:
for ch in Chars[i]:
s.squeeze(ch)</langsyntaxhighlight>
 
{{out}}
Line 1,819 ⟶ 2,844:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Unicode::UCD 'charinfo';
Line 1,839 ⟶ 2,864:
$phrase ne $squeeze ? 'True' : 'False',
length($squeeze), $squeeze
}</langsyntaxhighlight>
{{out}}
<pre>Original length: 0 <<<>>>
Line 1,870 ⟶ 2,895:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant tests = {"", " ",
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
`"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln `,"-",
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span>
"..1111111111111111111111111111111111111111111111111111111111111117777888","7",
<span style="color: #008000;">`"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln `</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-"</span><span style="color: #0000FF;">,</span>
"I never give 'em hell, I just tell the truth, and they think it's hell. ",".",
<span style="color: #008000;">"..1111111111111111111111111111111111111111111111111111111111111117777888"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"7"</span><span style="color: #0000FF;">,</span>
" --- Harry S Truman "," -r"},
<span style="color: #008000;">"I never give 'em hell, I just tell the truth, and they think it's hell. "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"."</span><span style="color: #0000FF;">,</span>
fmt = """
<span style="color: #008000;">" --- Harry S Truman "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" -r"</span><span style="color: #0000FF;">},</span>
length %2d input: <<<%s>>>
<span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
length %2d squeeze(%c): <<<%s>>>
length %2d input: &lt;&lt;&lt;%s&gt;&gt;&gt;
"""
length %2d squeeze(%c): &lt;&lt;&lt;%s&gt;&gt;&gt;
function squeeze(sequence s, integer ch)
"""</span>
if length(s)>1 then
<span style="color: #008080;">function</span> <span style="color: #000000;">squeeze</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span>
integer outdx = 1
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)></span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
object prev = s[1]
<span style="color: #004080;">integer</span> <span style="color: #000000;">outdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
for i=2 to length(s) do
<span style="color: #004080;">object</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
if s[i]!=prev or prev!=ch then
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
prev = s[i]
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">prev</span> <span style="color: #008080;">or</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">ch</span> <span style="color: #008080;">then</span>
outdx += 1
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
s[outdx] = prev
<span style="color: #000000;">outdx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
end if
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">outdx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">prev</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
s = s[1..outdx]
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">outdx</span><span style="color: #0000FF;">]</span>
return s
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
for i=1 to length(tests) by 2 do
string ti = tests[i], chars = tests[i+1]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
for j=1 to length(chars) do
<span style="color: #004080;">string</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">chars</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
string si = squeeze(ti,chars[j])
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">chars</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
printf(1,fmt,{length(ti),ti,length(si),chars[j],si})
<span style="color: #004080;">string</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">squeeze</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #000000;">chars</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">])</span>
end for
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">),</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">si</span><span style="color: #0000FF;">),</span><span style="color: #000000;">chars</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #000000;">si</span><span style="color: #0000FF;">})</span>
end for</lang>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,928 ⟶ 2,956:
=={{header|PHP}}==
 
<langsyntaxhighlight PHPlang="php"><?php
 
function squeezeString($string, $squeezeChar) {
Line 1,966 ⟶ 2,994:
echo 'Squeeze(', $char, ') : string is not squeezable (char=', $char, ')...', PHP_EOL, PHP_EOL;
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,991 ⟶ 3,019:
 
=={{header|Prolog}}==
<langsyntaxhighlight Prologlang="prolog">squeeze_( [], _, [] ).
squeeze_( [A], _, [A] ).
squeeze_( [A,A|T], A, R ) :- squeeze_( [A|T], A, R ).
Line 2,000 ⟶ 3,028:
string_chars( Str, Chars ),
squeeze_( Chars, SqueezeChar, Result ),
string_chars( Collapsed, Result ).</langsyntaxhighlight>
{{out}}
<pre>
Line 2,034 ⟶ 3,062:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import groupby
 
def squeezer(s, txt):
Line 2,059 ⟶ 3,087:
this = f"Squeezer '{ch}'"
sqz = squeezer(ch, txt)
print(f"{this:>14} Size: {len(sqz)} «««{sqz}»»»" )</langsyntaxhighlight>
 
{{out}}
Line 2,091 ⟶ 3,119:
Original Size: 8 «««😍😀🙌💃😍😍😍🙌»»»
Squeezer '😍' Size: 6 «««😍😀🙌💃😍🙌»»»</pre>
 
 
=={{header|R}}==
<syntaxhighlight lang="r">squeeze_string <- function(string, character){
str_iterable <- strsplit(string, "")[[1]]
message(paste0("Original String: ", "<<<", string, ">>>\n",
"Length: ", length(str_iterable), "\n",
"Character: ", character))
detect <- rep(TRUE, length(str_iterable))
for(i in 2:length(str_iterable)){
if(length(str_iterable) == 0) break
if(str_iterable[i] != character) next
if(str_iterable[i] == str_iterable[i-1])
detect[i] <- FALSE
}
squeezed_string <- paste(str_iterable[detect], collapse = "")
message(paste0("squeezed string: ", "<<<",squeezed_string, ">>>\n",
"Length: ", length(str_iterable[detect])), "\n")
}
squeeze_string("", " ")
squeeze_string("'If I were two-faced, would I be wearing this one?' --- Abraham Lincoln ", "-")
squeeze_string("..1111111111111111111111111111111111111111111111111111111111111117777888", "7")
squeeze_string("I never give 'em hell, I just tell the truth, and they think it's hell. ", ".")
squeeze_string(" --- Harry S Truman ", " ")
squeeze_string(" --- Harry S Truman ", "-")
squeeze_string(" --- Harry S Truman ", "r")
squeeze_string(" Ciao Mamma, guarda come mi diverto!!! ", "!")
</syntaxhighlight>
 
{{out}}
<pre>
Original String: <<<>>>
Length: 0
Character:
squeezed string: <<<>>>
Length: 0
Original String: <<<'If I were two-faced, would I be wearing this one?' --- Abraham Lincoln >>>
Length: 72
Character: -
squeezed string: <<<'If I were two-faced, would I be wearing this one?' - Abraham Lincoln >>>
Length: 70
Original String: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
Length: 72
Character: 7
squeezed string: <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>
Length: 69
 
Original String: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
Length: 72
Character: .
squeezed string: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
Length: 72
Original String: <<< --- Harry S Truman >>>
Length: 72
Character:
squeezed string: <<< --- Harry S Truman >>>
Length: 20
Original String: <<< --- Harry S Truman >>>
Length: 72
Character: -
squeezed string: <<< - Harry S Truman >>>
Length: 70
Original String: <<< --- Harry S Truman >>>
Length: 72
Character: r
squeezed string: <<< --- Hary S Truman >>>
Length: 71
Original String: <<< Ciao Mamma, guarda come mi diverto!!! >>>
Length: 41
Character: !
squeezed string: <<< Ciao Mamma, guarda come mi diverto! >>>
Length: 39
</pre>
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket/base
 
(define (squeeze-string s c)
(let loop ((cs (string->list s)) (squeezing? #f) (l 0) (acc null))
(cond [(null? cs) (values l (list->string (reverse acc)))]
[(and squeezing? (char=? (car cs) c)) (loop (cdr cs) #t l acc)]
[else (loop (cdr cs) (char=? (car cs) c) (add1 l) (cons (car cs) acc))])))
 
(define (report-squeeze s c)
(define-values (l′ s′) (squeeze-string s c))
(printf "Squeezing ~s in «««~a»»» (length ~a)~%" c s (string-length s))
(printf "Result: «««~a»»» (length ~a)~%~%" s′ l′))
 
(define (Determine-if-a-string-is-squeezeable)
(report-squeeze "" #\space)
(report-squeeze "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " #\-)
(report-squeeze "..1111111111111111111111111111111111111111111111111111111111111117777888" #\7)
(report-squeeze "I never give 'em hell, I just tell the truth, and they think it's hell. " #\.)
(define truman-sig " --- Harry S Truman ")
(report-squeeze truman-sig #\space)
(report-squeeze truman-sig #\-)
(report-squeeze truman-sig #\r))
 
(module+ main
(Determine-if-a-string-is-squeezeable))</syntaxhighlight>
 
{{out}}
 
<pre>Squeezing #\space in «««»»» (length 0)
Result: «««»»» (length 0)
 
Squeezing #\- in «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» (length 72)
Result: «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» (length 70)
 
Squeezing #\7 in «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» (length 72)
Result: «««..1111111111111111111111111111111111111111111111111111111111111117888»»» (length 69)
 
Squeezing #\. in «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length 72)
Result: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length 72)
 
Squeezing #\space in ««« --- Harry S Truman »»» (length 72)
Result: ««« --- Harry S Truman »»» (length 20)
 
Squeezing #\- in ««« --- Harry S Truman »»» (length 72)
Result: ««« - Harry S Truman »»» (length 70)
 
Squeezing #\r in ««« --- Harry S Truman »»» (length 72)
Result: ««« --- Hary S Truman »»» (length 71)</pre>
 
=={{header|Raku}}==
Line 2,096 ⟶ 3,267:
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>map {
my $squeeze = $^phrase;
sink $^reg;
Line 2,109 ⟶ 3,280:
' --- Harry S Truman ', ' ',
' --- Harry S Truman ', '-',
' --- Harry S Truman ', 'r'</langsyntaxhighlight>
{{out}}
<pre>Original length: 0 <<<>>>
Line 2,141 ⟶ 3,312:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program "squeezes" all immediately repeated characters in a string (or strings). */
@.= /*define a default for the @. array. */
#.1= ' '; @.1=
Line 2,170 ⟶ 3,341:
$= $ || _ /*append char., it's diff. */
end /*j*/
return $</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
Line 2,205 ⟶ 3,376:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">strings = ["",
'"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ',
"..1111111111111111111111111111111111111111111111111111111111111117777888",
Line 2,221 ⟶ 3,392:
puts
end
</syntaxhighlight>
</lang>
{{out}}
<pre>original: «««»»» (size 0)
Line 2,246 ⟶ 3,417:
See collapsible strings for the alternative implementation approach.
 
<langsyntaxhighlight Rustlang="rust">fn squeezable_string<'a>(s: &'a str, squeezable: char) -> impl Iterator<Item = char> + 'a {
let mut previous = None;
 
Line 2,283 ⟶ 3,454:
 
inputs.iter().for_each(|(input, c)| show(input, *c));
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func squeeze(str, c) {
str.gsub(Regex("(" + c.escape + ")" + '\1+'), {|s1| s1 })
}
Line 2,306 ⟶ 3,477:
}
say ''
}</langsyntaxhighlight>
{{out}}
<pre style="font-size: 80%">
Line 2,330 ⟶ 3,501:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl"># Set test data as a list pairing even and odd values
# as test string and squeeze character(s) respectively.
set test {
Line 2,358 ⟶ 3,529:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,393 ⟶ 3,564:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports System.Linq.Enumerable
 
Module Module1
Line 2,425 ⟶ 3,596:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>squeeze: ' '
Line 2,448 ⟶ 3,619:
old: 72 ««« --- Harry S Truman »»»
new: 71 ««« --- Hary S Truman »»»</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">// Returns squeezed string, original and new lengths in
// unicode code points (not normalized).
fn squeeze(s string, c string) (string, int, int) {
mut r := s.runes()
mut t := c.runes()[0]
le, mut del := r.len, 0
for i := le - 2; i >= 0; i-- {
if r[i] == t && r[i] == r[i+1] {
r.delete(i)
del++
}
}
if del == 0 {
return s, le, le
}
r = r[..le-del]
return r.string(), le, r.len
}
fn main() {
strings := [
"",
'"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 ",
"The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
"headmistressship",
"aardvark",
"😍😀🙌💃😍😍😍🙌",
]
chars := [[' '], ['-'], ['7'], ['.'], [' ', '-', 'r'], ['e'], ['s'], ['a'], ['😍']]
for i, s in strings {
for c in chars[i] {
ss, olen, slen := squeeze(s, c)
println("specified character = $c")
println("original : length = ${olen:2}, string = «««$s»»»")
println("squeezed : length = ${slen:2}, string = «««$ss»»»\n")
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
specified character =
original : length = 0, string = «««»»»
squeezed : length = 0, string = «««»»»
 
specified character = -
original : length = 72, string = «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
squeezed : length = 70, string = «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
 
specified character = 7
original : length = 72, string = «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
squeezed : length = 69, string = «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
 
specified character = .
original : length = 72, string = «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
squeezed : length = 72, string = «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
 
specified character =
original : length = 72, string = ««« --- Harry S Truman »»»
squeezed : length = 20, string = ««« --- Harry S Truman »»»
 
specified character = -
original : length = 72, string = ««« --- Harry S Truman »»»
squeezed : length = 70, string = ««« - Harry S Truman »»»
 
specified character = r
original : length = 72, string = ««« --- Harry S Truman »»»
squeezed : length = 71, string = ««« --- Hary S Truman »»»
 
specified character = e
original : length = 80, string = «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»»
squeezed : length = 79, string = «««The better the 4-whel drive, the further you'll be from help when ya get stuck!»»»
 
specified character = s
original : length = 16, string = «««headmistressship»»»
squeezed : length = 14, string = «««headmistreship»»»
 
specified character = a
original : length = 8, string = «««aardvark»»»
squeezed : length = 7, string = «««ardvark»»»
 
specified character = 😍
original : length = 8, string = «««😍😀🙌💃😍😍😍🙌»»»
squeezed : length = 6, string = «««😍😀🙌💃😍🙌»»»
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
// Returns squeezed string, original and new lengths in
Line 2,490 ⟶ 3,753:
}
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
Line 2,537 ⟶ 3,800:
original : length = 8, string = «««😍😀🙌💃😍😍😍🙌»»»
squeezed : length = 6, string = «««😍😀🙌💃😍🙌»»»
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">string 0;
char C, I, J, Last;
 
proc Squeeze(Char, S); \Eliminate specified repeated characters from string
char Char, S;
[I:= 0; J:= 0; Last:= -1;
loop [if S(I) # Last or Char # Last then
[C(J):= S(I);
if S(I) = 0 then quit;
J:= J+1;
];
Last:= S(I);
I:= I+1;
];
];
 
int String, K, Char;
[String:= [
"",
"^"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 "];
Char:= [0, ^-, ^1, ^l, ^ , ^-, ^r];
C:= Reserve(79+1); \space for collapsed string
for K:= 0 to 4+2 do
[Squeeze(Char(K), String(if K>4 then 4 else K));
Text(0, "<<<"); Text(0, String(if K>4 then 4 else K)); Text(0, ">>> ");
IntOut(0, I); CrLf(0);
Text(0, "<<<"); Text(0, C); Text(0, ">>> "); IntOut(0, J); CrLf(0);
];
]</syntaxhighlight>
 
{{out}}
<pre>
<<<>>> 0
<<<>>> 0
<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> 72
<<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> 70
<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> 72
<<<..17777888>>> 10
<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> 72
<<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> 69
<<< --- Harry S Truman >>> 72
<<< --- Harry S Truman >>> 20
<<< --- Harry S Truman >>> 72
<<< - Harry S Truman >>> 70
<<< --- Harry S Truman >>> 72
<<< --- Hary S Truman >>> 71
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn squeeze(c,str){ // Works with UTF-8
s,cc,sz,n := Data(Void,str), String(c,c), c.len(), 0; // byte buffer in case of LOTs of deletes
while(Void != (n=s.find(cc,n))){ str=s.del(n,sz) } // and searching is faster for big strings
s.text
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">strings:=T(
T("",""),
T("-","\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "),
Line 2,563 ⟶ 3,878:
sstr:=squeeze(c,s);
println("After: %2d <<<%s>>>\n".fmt(sstr.len(-8),sstr));
}</langsyntaxhighlight>
{{out}}
<pre>
2,054

edits