Determine if a string is collapsible: Difference between revisions

From Rosetta Code
Content added Content deleted
(โ†’โ€Ž{{header|REXX}}: marked incorrect)
m (โ†’โ€Ž{{header|REXX}}: add semaphore.)
Line 1,414: Line 1,414:


=={{header|REXX}}==
=={{header|REXX}}==
{{incorrect|HQ9+|not consistent with the task's requirements which are a function which 'determines if a character string is collapsible' and 'if so collapses the string (by removing immediately repeated characters)}}
<lang rexx>/*REXX program "collapses" all immediately repeated characters in a string (or strings).*/
<lang rexx>/*REXX program "collapses" all immediately repeated characters in a string (or strings).*/
@.= /*define a default for the @. array. */
@.= /*define a default for the @. array. */
Line 1,429: Line 1,428:
say copies('โ•', 105) /*show a separator line between outputs*/
say copies('โ•', 105) /*show a separator line between outputs*/
if j>1 & L==0 then leave /*if arg is null and J>1, then leave. */
if j>1 & L==0 then leave /*if arg is null and J>1, then leave. */
say ' length='right(L, 3) " input=ยซยซยซ" || @.j || 'ยปยปยป'
new= collapse(@.j)
new= collapse(@.j)
say 'string' word("isn't is",1+collapsible) 'collapsible' /*display semaphore value*/
say ' length='right(L, 3) " input=ยซยซยซ" || @.j || 'ยปยปยป'
w= length(new)
w= length(new)
say ' length='right(w, 3) " output=ยซยซยซ" || new || 'ยปยปยป'
say ' length='right(w, 3) " output=ยซยซยซ" || new || 'ยปยปยป'
end /*j*/
end /*j*/
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€*/
/*โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€*/
collapse: procedure; parse arg y 1 $ 2 /*get a value from arg; get 1st char. */
collapse: procedure expose collapsible; parse arg y 1 $ 2 /*get the arg; get 1st char.*/
do k=2 to length(y) /*traipse through almost all the chars.*/
do k=2 to length(y) /*traipse through almost all the chars.*/
_= substr(y, k, 1) /*pick a character from Y*/
_= substr(y, k, 1) /*pick a character from Y (1st arg). */
if _==right($, 1) then iterate /*Same character? Skip it.*/
if _==right($, 1) then iterate /*Is this the same character? Skip it.*/
$= $ || _ /*append char., it's diff.*/
$= $ || _ /*append the character, it's different.*/
end /*j*/
end /*j*/
return $</lang>
collapsible= y\==$; return $ /*set boolean to true if collapsible.*/</lang>
{{out|output|text=&nbsp; when using the internal default inputs:}}
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
<pre>
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
string isn't collapsible
length= 0 input=ยซยซยซยปยปยป
length= 0 input=ยซยซยซยปยปยป
length= 0 output=ยซยซยซยปยปยป
length= 0 output=ยซยซยซยปยปยป
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
string is collapsible
length= 72 input=ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป
length= 72 input=ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป
length= 70 output=ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป
length= 70 output=ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
string is collapsible
length= 72 input=ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป
length= 72 input=ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป
length= 4 output=ยซยซยซ.178ยปยปยป
length= 4 output=ยซยซยซ.178ยปยปยป
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
string is collapsible
length= 72 input=ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป
length= 72 input=ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป
length= 69 output=ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป
length= 69 output=ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
string is collapsible
length= 72 input=ยซยซยซ --- Harry S Truman ยปยปยป
length= 72 input=ยซยซยซ --- Harry S Truman ยปยปยป
length= 17 output=ยซยซยซ - Hary S Truman ยปยปยป
length= 17 output=ยซยซยซ - Hary S Truman ยปยปยป

Revision as of 21:41, 18 June 2020

Task
Determine if a string is collapsible
You are encouraged to solve this task according to the task description, using any language you may know.

Determine if a character string is   collapsible.

And if so,   collapse the string   (by removing   immediately repeated   characters).


If a character string has   immediately repeated   character(s),   the repeated characters are to be deleted (removed),   but not the primary (1st) character(s).


An   immediately repeated   character is any character that is   immediately   followed by an identical character (or characters).   Another word choice could've been   duplicated character,   but that might have ruled out   (to some readers)   triplicated characters   ยทยทยท   or more.


{This Rosetta Code task was inspired by a newly introduced   (as of around November 2019)   PL/I   BIF:   collapse.}


Examples

In the following character string:


 The better the 4-wheel drive, the further you'll be from help when ya get stuck! 


Only the 2nd   t,   e, and   l   are repeated characters,   indicated by underscores (above),   even though they (those characters) appear elsewhere in the character string.


So, after collapsing the string, the result would be:

 The beter the 4-whel drive, the further you'l be from help when ya get stuck! 



Another example: In the following character string:

 headmistressship 


The "collapsed" string would be:

 headmistreship 


Task

Write a subroutine/function/procedure/routineยทยทยท   to locate   repeated   characters and   collapse   (delete)   them from the character string.   The character string can be processed from either direction.


Show all output here, on this page:

  •   the   original string and its length
  •   the resultant string and its length
  •   the above strings should be "bracketed" with   <<<   and   >>>   (to delineate blanks)
  •   ยซยซยซGuillemets may be used instead for "bracketing" for the more artistic programmers,   shown used hereยปยปยป


Use (at least) the following five strings,   all strings are length seventy-two (characters, including blanks),   except the 1st string:

 string
 number
        โ•”โ•—
   1    โ•‘โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—   โ—„โ– โ– โ– โ– โ– โ–   a null string  (length zero)
   2    โ•‘"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln โ•‘
   3    โ•‘..1111111111111111111111111111111111111111111111111111111111111117777888โ•‘
   4    โ•‘I never give 'em hell, I just tell the truth, and they think it's hell. โ•‘
   5    โ•‘                                                    --- Harry S Truman  โ•‘   โ—„โ– โ– โ– โ– โ– โ–   has many repeated blanks
        โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•



Related tasks



Ada

<lang Ada>with Ada.Text_IO; use Ada.Text_IO; procedure Test_Collapsible is

  procedure Collapse (S : in String) is
     Res : String (1 .. S'Length);
     Len : Natural := 0;
  begin
     Put_Line ("Input  = <<<" & S & ">>>, length =" & S'Length'Image);
     for I in S'Range loop
        if Len = 0 or else S(I) /= Res(Len) then
           Len := Len + 1;
           Res(Len) := S(I);
        end if;
     end loop;
     Put_Line ("Output = <<<" & Res (1 .. Len) & ">>>, length =" & Len'Image);
  end Collapse;

begin

  Collapse ("");
  Collapse ("""If I were two-faced, would I be wearing this one?"" --- Abraham Lincoln ");
  Collapse ("..1111111111111111111111111111111111111111111111111111111111111117777888");
  Collapse ("I never give 'em hell, I just tell the truth, and they think it's hell. ");
  Collapse ("                                                    --- Harry S Truman  ");

end Test_Collapsible; </lang>

Output:
Input  = <<<>>>, length = 0
Output = <<<>>>, length = 0
Input  = <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>, length = 72
Output = <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>, length = 70
Input  = <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length = 72
Output = <<<.178>>>, length = 4
Input  = <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72
Output = <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>, length = 69
Input  = <<<                                                    --- Harry S Truman  >>>, length = 72
Output = <<< - Hary S Truman >>>, length = 17

AWK

<lang AWK>

  1. 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>

Output:
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>>>

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.

<lang C>

  1. include<string.h>
  2. include<stdlib.h>
  3. include<stdio.h>
  1. define COLLAPSE 0
  2. define SQUEEZE 1

typedef struct charList{

   char c;
   struct charList *next;

} charList;

/* Implementing strcmpi, the case insensitive string comparator, as it is not part of the C Standard Library.

Comment this out if testing on a compiler where it is already defined.

  • /

int strcmpi(char* str1,char* str2){

   int len1 = strlen(str1), len2 = strlen(str2), i;
   if(len1!=len2){
       return 1;
   }
   else{
       for(i=0;i<len1;i++){
           if((str1[i]>='A'&&str1[i]<='Z')&&(str2[i]>='a'&&str2[i]<='z')&&(str2[i]-65!=str1[i]))
               return 1;
           else if((str2[i]>='A'&&str2[i]<='Z')&&(str1[i]>='a'&&str1[i]<='z')&&(str1[i]-65!=str2[i]))
               return 1;
           else if(str1[i]!=str2[i])
               return 1;
       }
   }
   return 0;

}

charList *strToCharList(char* str){

   int len = strlen(str),i;
   charList *list, *iterator, *nextChar;
   list = (charList*)malloc(sizeof(charList));
   list->c = str[0];
   list->next = NULL;
   iterator = list;
   for(i=1;i<len;i++){
       nextChar = (charList*)malloc(sizeof(charList));
       nextChar->c = str[i];
       nextChar->next = NULL;
       iterator->next = nextChar;
       iterator = nextChar;
   }
   return list;

}

char* charListToString(charList* list){

   charList* iterator = list;
   int count = 0,i;
   char* str;
   while(iterator!=NULL){
       count++;
       iterator = iterator->next;
   }
   str = (char*)malloc((count+1)*sizeof(char));
   iterator = list;
   for(i=0;i<count;i++){
       str[i] = iterator->c;
       iterator = iterator->next;
   }
   free(list);
   str[i] = '\0';
   return str;

}

char* processString(char str[100],int operation, char squeezeChar){

   charList *strList = strToCharList(str),*iterator = strList, *scout;
   if(operation==SQUEEZE){
       while(iterator!=NULL){
           if(iterator->c==squeezeChar){
               scout = iterator->next;
               while(scout!=NULL && scout->c==squeezeChar){
                       iterator->next = scout->next;
                       scout->next = NULL;
                       free(scout);
                       scout = iterator->next;
               }
           }
           iterator = iterator->next;
       }
   }
   else{
       while(iterator!=NULL && iterator->next!=NULL){
           if(iterator->c == (iterator->next)->c){
               scout = iterator->next;
               squeezeChar = iterator->c;
               while(scout!=NULL && scout->c==squeezeChar){
                       iterator->next = scout->next;
                       scout->next = NULL;
                       free(scout);
                       scout = iterator->next;
               }
           }
           iterator = iterator->next;
       }
   }
   return charListToString(strList);

}

void printResults(char originalString[100], char finalString[100], int operation, char squeezeChar){

   if(operation==SQUEEZE){
       printf("Specified Operation : SQUEEZE\nTarget Character : %c",squeezeChar);
   }
   else
       printf("Specified Operation : COLLAPSE");
   printf("\nOriginal %c%c%c%s%c%c%c\nLength : %d",174,174,174,originalString,175,175,175,(int)strlen(originalString));
   printf("\nFinal    %c%c%c%s%c%c%c\nLength : %d\n",174,174,174,finalString,175,175,175,(int)strlen(finalString));

}

int main(int argc, char** argv){

   int operation;
   char squeezeChar;
   if(argc<3||argc>4){
       printf("Usage : %s <SQUEEZE|COLLAPSE> <String to be processed> <Character to be squeezed, if operation is SQUEEZE>\n",argv[0]);
       return 0;
   }
   if(strcmpi(argv[1],"SQUEEZE")==0 && argc!=4){
       scanf("Please enter characted to be squeezed : %c",&squeezeChar);
       operation = SQUEEZE;
   }
   else if(argc==4){
       operation = SQUEEZE;
       squeezeChar = argv[3][0];
   }
   else if(strcmpi(argv[1],"COLLAPSE")==0){
       operation = COLLAPSE;
   }
   if(strlen(argv[2])<2){
       printResults(argv[2],argv[2],operation,squeezeChar);
   }
   else{
       printResults(argv[2],processString(argv[2],operation,squeezeChar),operation,squeezeChar);
   }
   
   return 0;

} </lang> Output :

C:\My Projects\networks>a collapse "The better the 4-wheel drive, the further you'll be from help when ya get stuck!"
Specified Operation : COLLAPSE
Original ยซยซยซThe better the 4-wheel drive, the further you'll be from help when ya get stuck!ยปยปยป
Length : 80
Final    ยซยซยซThe beter the 4-whel drive, the further you'l be from help when ya get stuck!ยปยปยป
Length : 77

C:\My Projects\networks>a collapse headmistressship
Specified Operation : COLLAPSE
Original ยซยซยซheadmistressshipยปยปยป
Length : 16
Final    ยซยซยซheadmistreshipยปยปยป
Length : 14

C:\My Projects\networks>a collapse ""
Specified Operation : COLLAPSE
Original ยซยซยซยปยปยป
Length : 0
Final    ยซยซยซยปยปยป
Length : 0

C:\My Projects\networks>a collapse "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln"
Specified Operation : COLLAPSE
Original ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincolnยปยปยป
Length : 71
Final    ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincolnยปยปยป
Length : 69

C:\My Projects\networks>a collapse ..1111111111111111111111111111111111111111111111111111111111111117777888
Specified Operation : COLLAPSE
Original ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป
Length : 72
Final    ยซยซยซ.178ยปยปยป
Length : 4

C:\My Projects\networks>a collapse "I never give 'em hell, I just tell the truth, and they think it's hell."
Specified Operation : COLLAPSE
Original ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell.ยปยปยป
Length : 71
Final    ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel.ยปยปยป
Length : 68

C:\My Projects\networks>a collapse "                                                    --- Harry S Truman"
Specified Operation : COLLAPSE
Original ยซยซยซ                                                    --- Harry S Trumanยปยปยป
Length : 70
Final    ยซยซยซ - Hary S Trumanยปยปยป
Length : 16

C#

<lang csharp>using System; using static System.Linq.Enumerable;

public class Program {

   static void Main()
   {
       string[] input = {
           "",
           "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
           "headmistressship",
           "\"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  "
       };
       foreach (string s in input) {
           Console.WriteLine($"old: {s.Length} ยซยซยซ{s}ยปยปยป");
           string c = Collapse(s);
           Console.WriteLine($"new: {c.Length} ยซยซยซ{c}ยปยปยป");
       }
   }
   static string Collapse(string s) => string.IsNullOrEmpty(s) ? "" :
       s[0] + new string(Range(1, s.Length - 1).Where(i => s[i] != s[i - 1]).Select(i => s[i]).ToArray());

}</lang>

Output:
old: 0 ยซยซยซยปยปยป
new: 0 ยซยซยซยปยปยป
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ยปยปยป
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 ยปยปยป

C++

The solution is a straightforward application of the standard library function "unique". <lang cpp>#include <string>

  1. include <iostream>
  2. include <algorithm>

template<typename char_type> std::basic_string<char_type> collapse(std::basic_string<char_type> str) {

   auto i = std::unique(str.begin(), str.end());
   str.erase(i, str.end());
   return str;

}

void test(const std::string& str) {

   std::cout << "original string: <<<" << str << ">>>, length = " << str.length() << '\n';
   std::string collapsed(collapse(str));
   std::cout << "result string: <<<" << collapsed << ">>>, length = " << collapsed.length() << '\n';
   std::cout << '\n';

}

int main(int argc, char** argv) {

   test("");
   test("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ");
   test("..1111111111111111111111111111111111111111111111111111111111111117777888");
   test("I never give 'em hell, I just tell the truth, and they think it's hell. ");
   test("                                                    --- Harry S Truman  ");
   return 0;

}</lang>

Output:
original string: <<<>>>, length = 0
result string: <<<>>>, length = 0

original string: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>, length = 72
result string: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>, length = 70

original string: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length = 72
result string: <<<.178>>>, length = 4

original string: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72
result string: <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>, length = 69

original string: <<<                                                    --- Harry S Truman  >>>, length = 72
result string: <<< - Hary S Truman >>>, length = 17

Clojure

<lang Clojure> (defn collapse [s]

 (let [runs (partition-by identity s)]
   (apply str (map first runs))))

(defn run-test [s]

 (let [out (collapse s)]
   (str (format "Input: <<<%s>>> (len %d)\n" s (count s))
        (format "becomes: <<<%s>>> (len %d)\n" out (count out)))))

</lang>

Output:
Input:   <<<>>> (len 0)
becomes: <<<>>> (len 0)
Input:   <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> (len 72)
becomes: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> (len 70)
Input:   <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (len 72)
becomes: <<<.178>>> (len 4)
Input:   <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (len 72)
becomes: <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> (len 69)
Input:   <<<                                                    --- Harry S Truman  >>> (len 72)
becomes: <<< - Hary S Truman >>> (len 17)

D

<lang d>import std.stdio;

void collapsible(string s) {

   writeln("old: <<<", s, ">>>, length = ", s.length);
   write("new: <<<");
   char last = '\0';
   int len = 0;
   foreach (c; s) {
       if (c != last) {
           write(c);
           len++;
       }
       last = c;
   }
   writeln(">>>, length = ", len);
   writeln;

}

void main() {

   collapsible(``);
   collapsible(`"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln `);
   collapsible(`..1111111111111111111111111111111111111111111111111111111111111117777888`);
   collapsible(`I never give 'em hell, I just tell the truth, and they think it's hell. `);
   collapsible(`                                                    --- Harry S Truman  `);

}</lang>

Output:
old: <<<>>>, length = 0
new: <<<>>>, length = 0

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

old: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length = 72
new: <<<.178>>>, length = 4

old: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72
new: <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>, length = 69

old: <<<                                                    --- Harry S Truman  >>>, length = 72
new: <<< - Hary S Truman >>>, length = 17

F#

<lang fsharp> // Collapse a String. Nigel Galloway: June 9th., 2020 //As per the task description a function which 'determines if a character string is collapsible' by testing if any consecutive characters are the same. let isCollapsible n=n|>Seq.pairwise|>Seq.tryFind(fun(n,g)->n=g) //As per the task description a function which 'if the string is collapsable, collapses the string (by removing immediately repeated characters). let collapse n=match isCollapsible n with

               Some _->let i=Seq.head n
                       let fN=let mutable g=i in (fun n->if n=g then false else g<-n; true)
                       let g=System.String([|yield i;yield! Seq.tail n|>Seq.filter fN|])
                       printfn "<<<%s>>> (length %d) colapses to <<<%s>>> (length %d)" n n.Length g g.Length
              |     _->printfn "<<<%s>>> (length %d) does not colapse" n n.Length

collapse "" collapse "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " collapse "..1111111111111111111111111111111111111111111111111111111111111117777888" collapse "I never give 'em hell, I just tell the truth, and they think it's hell. " collapse " --- Harry S Truman " collapse "withoutConsecutivelyRepeatedCharacters" </lang>

Output:
<<<>>> (length 0) does not colapse
<<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> (length 72) collapses to <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> (length 70)
<<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (length 72) collapses to <<<.178>>> (length 4)
<<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (length 72) collapses to <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> (length 69)
<<<                                                    --- Harry S Truman  >>> (length 72) collapses to <<< - Hary S Truman >>> (length 17)
<<<withoutConsecutivelyRepeatedCharacters>>> (length 38) does not collapse

Factor

<lang factor>USING: formatting io kernel sbufs sequences strings ; IN: rosetta-code.string-collapse

(collapse) ( str -- str )
   unclip-slice 1string >sbuf
   [ over last over = [ drop ] [ suffix! ] if ] reduce >string ;
collapse ( str -- str ) [ "" ] [ (collapse) ] if-empty ;
.str ( str -- ) dup length "ยซยซยซ%sยปยปยป (length %d)\n" printf ;
show-collapse ( str -- )
   [ "Before collapse: " write .str ]
   [ "After  collapse: " write collapse .str ] bi nl ;
collapse-demo ( -- )
   {
       ""
       "\"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"
       "๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ"
   } [ show-collapse ] each ;

MAIN: collapse-demo</lang>

Output:

(Using some extra test cases from the Go entry.)

Before collapse: ยซยซยซยปยปยป (length 0)
After  collapse: ยซยซยซยปยปยป (length 0)

Before collapse: ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป (length 72)
After  collapse: ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป (length 70)

Before collapse: ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป (length 72)
After  collapse: ยซยซยซ.178ยปยปยป (length 4)

Before collapse: ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป (length 72)
After  collapse: ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป (length 69)

Before collapse: ยซยซยซ                                                   ---  Harry S Truman  ยปยปยป (length 72)
After  collapse: ยซยซยซ - Hary S Truman ยปยปยป (length 17)

Before collapse: ยซยซยซThe better the 4-wheel drive, the further you'll be from help when ya get stuck!ยปยปยป (length 80)
After  collapse: ยซยซยซThe beter the 4-whel drive, the further you'l be from help when ya get stuck!ยปยปยป (length 77)

Before collapse: ยซยซยซheadmistressshipยปยปยป (length 16)
After  collapse: ยซยซยซheadmistreshipยปยปยป (length 14)

Before collapse: ยซยซยซaardvarkยปยปยป (length 8)
After  collapse: ยซยซยซardvarkยปยปยป (length 7)

Before collapse: ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œยปยปยป (length 8)
After  collapse: ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ™Œยปยปยป (length 6)

Go

<lang go>package main

import "fmt"

// Returns collapsed string, original and new lengths in // unicode code points (not normalized). func collapse(s string) (string, int, int) {

   r := []rune(s)
   le, del := len(r), 0
   for i := le - 2; i >= 0; i-- {
       if r[i] == r[i+1] {
           copy(r[i:], r[i+1:])
           del++
       }
   }
   if del == 0 {
       return s, le, le
   }
   r = r[:le-del]
   return string(r), le, len(r)

}

func main() {

   strings:= []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  ",
       "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
       "headmistressship",
       "aardvark",
       "๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ",
   }
   for _, s := range strings {
       cs, olen, clen := collapse(s)
       fmt.Printf("original : length = %2d, string = ยซยซยซ%sยปยปยป\n", olen, s)
       fmt.Printf("collapsed: length = %2d, string = ยซยซยซ%sยปยปยป\n\n", clen, cs)
   }

}</lang>

Output:
original : length =  0, string = ยซยซยซยปยปยป
collapsed: length =  0, string = ยซยซยซยปยปยป

original : length = 72, string = ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป
collapsed: length = 70, string = ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป

original : length = 72, string = ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป
collapsed: length =  4, string = ยซยซยซ.178ยปยปยป

original : length = 72, string = ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป
collapsed: length = 69, string = ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป

original : length = 72, string = ยซยซยซ                                                   ---  Harry S Truman  ยปยปยป
collapsed: length = 17, string = ยซยซยซ - Hary S Truman ยปยปยป

original : length = 80, string = ยซยซยซThe better the 4-wheel drive, the further you'll be from help when ya get stuck!ยปยปยป
collapsed: length = 77, string = ยซยซยซThe beter the 4-whel drive, the further you'l be from help when ya get stuck!ยปยปยป

original : length = 16, string = ยซยซยซheadmistressshipยปยปยป
collapsed: length = 14, string = ยซยซยซheadmistreshipยปยปยป

original : length =  8, string = ยซยซยซaardvarkยปยปยป
collapsed: length =  7, string = ยซยซยซardvarkยปยปยป

original : length =  8, string = ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œยปยปยป
collapsed: length =  6, string = ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ™Œยปยปยป

Haskell

<lang haskell>import Text.Printf (printf) import Data.Maybe (catMaybes) import Control.Monad (guard)

input :: [String] input = [ ""

       , "The better the 4-wheel drive, the further you'll be from help when ya get stuck!"
       , "headmistressship"
       , "\"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 :: Eq a => [a] -> [a] collapse = catMaybes . (\xs -> zipWith (\a b -> guard (a /= b) >> a) (Nothing : xs) (xs <> [Nothing])) . map Just

main :: IO () main =

 mapM_ (\(a, b) -> printf "old: %3d ยซยซยซ%sยปยปยป\nnew: %3d ยซยซยซ%sยปยปยป\n\n" (length a) a (length b) b) 
 $ ((,) <*> collapse) <$> input</lang>
Output:
old:   0 ยซยซยซยปยปยป
new:   0 ยซยซยซยปยปยป

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ยปยปยป

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:   8 ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œยปยปยป
new:   6 ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ™Œยปยปยป

Note that we can also directly define a predicate, and a rewrite, in terms of Data.List group,

<lang haskell>import Data.List (group)

isCollapsible :: String -> Bool isCollapsible = any ((1 <) . length) . group

collapsed :: String -> String collapsed = map head . group</lang>

or, without imports, in terms of pattern matching:

<lang haskell>isCollapsible :: String -> Bool isCollapsible [] = False isCollapsible [_] = False isCollapsible (h:t@(x:_)) = h == x || isCollapsible t

collapsed :: String -> String collapsed [] = [] collapsed [x] = [x] collapsed (h:t@(x:_))

 | h == x = collapsed t
 | otherwise = h : collapsed t</lang>

J

   STRINGS =: <;._2]0 :0

"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 =: (#~ (1 , 2 ~:/\ ])) ::(''"_)  NB. copy dissimilar neighbors
   assert 1 2 3 2 3 1 2 1 -: collapse 1 2 3 2 2 2 2 3 1 1 2 2 1   NB. test

   task =: ,&(<@:(;~ (_6 + #)))&('<<<' , '>>>' ,~ ]) collapse  NB. assemble the output

   task&> STRINGS   NB. operate on the data
+-----------------------------------------------------------------------------------+---------------------------------------------------------------------------------+
|+-+------+                                                                         |+-+------+                                                                       |
||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 >>>|                                                     |
|+--+------------------------------------------------------------------------------+|+--+-----------------------+                                                     |
+-----------------------------------------------------------------------------------+---------------------------------------------------------------------------------+

Java

<lang java> // Title: Determine if a string is collapsible

public class StringCollapsible {

   public static void main(String[] args) {
       for ( String s : new 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  ",
             "122333444455555666666777777788888888999999999",
             "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
             "headmistressship"}) {
           String result = collapse(s);
           System.out.printf("old:  %2d <<<%s>>>%nnew:  %2d <<<%s>>>%n%n", s.length(), s, result.length(), result);
       }
   }
   
   private static String collapse(String in) {
       StringBuilder sb = new StringBuilder();
       for ( int i = 0 ; i < in.length() ; i++ ) {
           if ( i == 0 || in.charAt(i-1) != in.charAt(i) ) {
               sb.append(in.charAt(i));
           }
       }
       return sb.toString();
   }

} </lang>

Output:
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:  45 <<<122333444455555666666777777788888888999999999>>>
new:   9 <<<123456789>>>

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>>>

Julia

<lang julia>const 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  "]

function collapse(s)

   len = length(s)
   if len < 2
       return s, len, s, len
   end
   result = last = s[1]
   for c in s[2:end]
       if c != last
           last = c
           result *= c
       end
   end
   return s, len, result, length(result)

end

function testcollapse(arr)

   for s in arr
       (s1, len1, s2, len2) = collapse(s)
       println("ยซยซยซ$s1ยปยปยป (length $len1)\n    collapses to:\nยซยซยซ$s2ยปยปยป (length $len2).\n")
   end

end

testcollapse(teststrings)

</lang>

Output:
ยซยซยซยปยปยป (length 0)
    collapses to:
ยซยซยซยปยปยป (length 0).

ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป (length 72)
    collapses to:
ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป (length 70).

ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป (length 72)
    collapses to:
ยซยซยซ.178ยปยปยป (length 4).

ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป (length 72)
    collapses to:
ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป (length 69).

ยซยซยซ                                                    --- Harry S Truman  ยปยปยป (length 72)
    collapses to:
ยซยซยซ - Hary S Truman ยปยปยป (length 17).

Condensed version:

<lang julia>const 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  "]

collapse(s) = (t = isempty(s) ? "" : s[1:1]; for c in s if c != t[end] t *= c end; end; t)

for s in teststrings

   n, t = length(s), collapse(s)
   println("ยซยซยซ$sยปยปยป (length $n)\n    collapses to:\nยซยซยซ$tยปยปยป (length $(length(t))).\n")

end </lang>

Kotlin

Translation of: Go

<lang scala>fun collapse(s: String): String {

   val cs = StringBuilder()
   var last: Char = 0.toChar()
   for (c in s) {
       if (c != last) {
           cs.append(c)
           last = c
       }
   }
   return cs.toString()

}

fun main() {

   val strings = arrayOf(
       "",
       "\"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"
   )
   for (s in strings) {
       val c = collapse(s)
       println("original : length = ${s.length}, string = ยซยซยซ$sยปยปยป")
       println("collapsed : length = ${c.length}, string = ยซยซยซ$cยปยปยป")
       println()
   }

}</lang>

Output:
original : length = 0, string = ยซยซยซยปยปยป
collapsed : length = 0, string = ยซยซยซยปยปยป

original : length = 72, string = ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป
collapsed : length = 70, string = ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป

original : length = 72, string = ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป
collapsed : length = 4, string = ยซยซยซ.178ยปยปยป

original : length = 72, string = ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป
collapsed : length = 69, string = ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป

original : length = 72, string = ยซยซยซ                                                   ---  Harry S Truman  ยปยปยป
collapsed : length = 17, string = ยซยซยซ - Hary S Truman ยปยปยป

original : length = 80, string = ยซยซยซThe better the 4-wheel drive, the further you'll be from help when ya get stuck!ยปยปยป
collapsed : length = 77, string = ยซยซยซThe beter the 4-whel drive, the further you'l be from help when ya get stuck!ยปยปยป

original : length = 16, string = ยซยซยซheadmistressshipยปยปยป
collapsed : length = 14, string = ยซยซยซheadmistreshipยปยปยป

original : length = 8, string = ยซยซยซaardvarkยปยปยป
collapsed : length = 7, string = ยซยซยซardvarkยปยปยป

Perl

<lang perl>use strict; use warnings; use utf8; binmode STDOUT, ":utf8";

my @lines = split "\n", <<~'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 American people have a right to know if their president is a crook.
                                                       --- Richard Nixon
   Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘
   AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
   STRINGS

for (@lines) {

   my $squish = s/(.)\1+/$1/gr;
   printf "\nLength: %2d <<<%s>>>\nCollapsible: %s\nLength: %2d <<<%s>>>\n",
     length($_), $_, $_ ne $squish ? 'True' : 'False', length($squish), $squish

}</lang>

Output:
Length:  0 <<<>>>
Collapsible: False
Length:  0 <<<>>>

Length: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
Collapsible: True
Length: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>

Length: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
Collapsible: True
Length:  4 <<<.178>>>

Length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
Collapsible: True
Length: 69 <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>

Length: 72 <<<                                                    --- Harry S Truman  >>>
Collapsible: True
Length: 17 <<< - Hary S Truman >>>

Length: 72 <<<The American people have a right to know if their president is a crook. >>>
Collapsible: True
Length: 71 <<<The American people have a right to know if their president is a crok. >>>

Length: 72 <<<                                                    --- Richard Nixon   >>>
Collapsible: True
Length: 17 <<< - Richard Nixon >>>

Length: 120 <<<Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘>>>
Collapsible: False
Length: 120 <<<Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘>>>

Length: 72 <<<AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA>>>
Collapsible: True
Length:  1 <<<A>>>

Phix

There is already a builtin we can abuse for this. The documentation of unique() [added for 0.8.1] states:
`If "PRESORTED" is specified, but s is not actually sorted, then only adjacent duplicates are removed.`
If you don't have builtins/punique.e to hand, you may want to take a quick look at squeeze() in [[1]], but obviously w/o ch. <lang Phix>constant tests = {"",

                 `"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  "},
        fmt = """		

length %2d input: <<<%s>>> length %2d output: <<<%s>>> """ for i=1 to length(tests) do

   string ti = tests[i], ci = unique(ti, "PRESORTED")
   printf(1,fmt,{length(ti),ti,length(ci),ci})

end for</lang>

Output:
length  0  input: <<<>>>
length  0 output: <<<>>>

length 72  input: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
length 70 output: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>

length 72  input: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
length  4 output: <<<.178>>>

length 72  input: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
length 69 output: <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>

length 72  input: <<<                                                    --- Harry S Truman  >>>
length 17 output: <<< - Hary S Truman >>>

PHP

<lang PHP><?php

function collapseString($string) {

   $previousChar = null;
   $collapse = ;
   $charArray = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
   for ($i = 0 ; $i < count($charArray) ; $i++) {
       $currentChar = $charArray[$i];
       if ($previousChar !== $currentChar) {
           $collapse .= $charArray[$i];
       }
       $previousChar = $currentChar;
   }
   return $collapse;

}

function isCollapsible($string) {

   return ($string !== collapseString($string));

}

$strings = array(

   ,
   'another non colapsing 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  ',
   '0112223333444445555556666666777777778888888889999999999',
   "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
   'headmistressship',
   "๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ",

);

foreach ($strings as $original) {

   echo 'Original : <<<', $original, '>>> (len=', mb_strlen($original), ')', PHP_EOL;
   if (isCollapsible($original)) {
       $collapse = collapseString($original);
       echo 'Collapse : <<<', $collapse, '>>> (len=', mb_strlen($collapse), ')', PHP_EOL, PHP_EOL;
   } else {
       echo 'Collapse : string is not collapsing...', PHP_EOL, PHP_EOL;
   }

}</lang>

Output:
Original : <<<>>> (len=0)
Collapse : string is not collapsing...

Original : <<<another non colapsing string>>> (len=28)
Collapse : string is not collapsing...

Original : <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> (len=72)
Collapse : <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> (len=70)

Original : <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (len=72)
Collapse : <<<.178>>> (len=4)

Original : <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (len=72)
Collapse : <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> (len=69)

Original : <<<                                                    --- Harry S Truman  >>> (len=72)
Collapse : <<< - Hary S Truman >>> (len=17)

Original : <<<0112223333444445555556666666777777778888888889999999999>>> (len=55)
Collapse : <<<0123456789>>> (len=10)

Original : <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>> (len=80)
Collapse : <<<The beter the 4-whel drive, the further you'l be from help when ya get stuck!>>> (len=77)

Original : <<<headmistressship>>> (len=16)
Collapse : <<<headmistreship>>> (len=14)

Original : <<<๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ>>> (len=8)
Collapse : <<<๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ™Œ>>> (len=6)

Prolog

<lang prolog>collapse_( [], [] ). collapse_( [A], [A] ). collapse_( [A,A|T], R ) :- collapse_( [A|T], R ). collapse_( [A,B|T], [A|R] ) :- dif( A, B ), collapse_( [B|T], R ).

collapse( Str, Collapsed ) :-

   string_chars( Str, Chars ),
   collapse_( Chars, Result ),
   string_chars( Collapsed, Result ).</lang>
Output:
?- collapse("122333444455555666666777777788888888999999999", New).
New = "123456789" .

?- collapse("", New).
New = "" .

?- collapse("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ", New).
New = "\"If I were two-faced, would I be wearing this one?\" - Abraham Lincoln " .

?- collapse("..1111111111111111111111111111111111111111111111111111111111111117777888", New).
New = ".178" .

?- collapse("I never give 'em hell, I just tell the truth, and they think it's hell. ", New).
New = "I never give 'em hel, I just tel the truth, and they think it's hel. " .

?- collapse("                                                    --- Harry S Truman  ", New).
New = " - Hary S Truman " .

?- collapse("The better the 4-wheel drive, the further you'll be from help when ya get stuck!", New).
New = "The beter the 4-whel drive, the further you'l be from help when ya get stuck!" .

33 ?- collapse("headmistressship", New).
New = "headmistreship" .

To determine if a string is collapsed or not, query if the result string is the same as the starting string. eg:

35 ?- S = "122333444455555666666777777788888888999999999", collapse(S,S).
false.

37 ?- S = "123456789", collapse(S,S).
S = "123456789" .

Python

<lang python>from itertools import groupby

def collapser(txt):

   return .join(item for item, grp in groupby(txt))

if __name__ == '__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",
           "๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ",
           ]
   for txt in strings:
       this = "Original"
       print(f"\n{this:14} Size: {len(txt)} ยซยซยซ{txt}ยปยปยป" )
       this = "Collapsed"
       sqz = collapser(txt)
       print(f"{this:>14} Size: {len(sqz)} ยซยซยซ{sqz}ยปยปยป" )</lang>
Output:
Original       Size: 0 ยซยซยซยปยปยป
     Collapsed Size: 0 ยซยซยซยปยปยป

Original       Size: 72 ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป
     Collapsed Size: 70 ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป

Original       Size: 72 ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป
     Collapsed Size: 4 ยซยซยซ.178ยปยปยป

Original       Size: 72 ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป
     Collapsed Size: 69 ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป

Original       Size: 72 ยซยซยซ                                                   ---  Harry S Truman  ยปยปยป
     Collapsed Size: 17 ยซยซยซ - Hary S Truman ยปยปยป

Original       Size: 80 ยซยซยซThe better the 4-wheel drive, the further you'll be from help when ya get stuck!ยปยปยป
     Collapsed Size: 77 ยซยซยซThe beter the 4-whel drive, the further you'l be from help when ya get stuck!ยปยปยป

Original       Size: 16 ยซยซยซheadmistressshipยปยปยป
     Collapsed Size: 14 ยซยซยซheadmistreshipยปยปยป

Original       Size: 8 ยซยซยซaardvarkยปยปยป
     Collapsed Size: 7 ยซยซยซardvarkยปยปยป

Original       Size: 8 ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œยปยปยป
     Collapsed Size: 6 ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ™Œยปยปยป


and for the missing predicate, foregrounded in the task title, and already forgotten in the listing of tests:

<lang python>Determining if a string is collapsible

from operator import eq


  1. isCollapsible :: String -> Bool

def isCollapsible(s):

   True if s contains any consecutively
      repeated characters.
   
   return False if 2 > len(s) else (
       any(map(eq, s, s[1:]))
   )


  1. ------------------------- TEST --------------------------
  2. main :: IO ()

def main():

   Determining whether each string is collapsible
   xs = [
       "",
       '"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",
       "๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ",
       "abcdefghijklmnopqrstuvwxyz"
   ]
   print([
       isCollapsible(x) for x in xs
   ])


  1. MAIN ---

if __name__ == '__main__':

   main()</lang>
Output:
[False, True, True, True, True, True, True, True, True, False]

Raku

(formerly Perl 6)

Works with: Rakudo version 2019.07.1

Technically, the task is asking for a boolean. "Determine if a string is collapsible" is answerable with True/False, so return a boolean as well.

<lang perl6>map {

   my $squish = .comb.squish.join;
   printf "\nLength: %2d <<<%s>>>\nCollapsible: %s\nLength: %2d <<<%s>>>\n",
     .chars, $_, .chars != $squish.chars, $squish.chars, $squish

}, lines

q:to/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 American people have a right to know if their president is a crook. 
                                                       --- Richard Nixon   
   Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘
   AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
   STRINGS

</lang>

Output:
Length:  0 <<<>>>
Collapsible: False
Length:  0 <<<>>>

Length: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
Collapsible: True
Length: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>

Length: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
Collapsible: True
Length:  4 <<<.178>>>

Length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
Collapsible: True
Length: 69 <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>>

Length: 72 <<<                                                    --- Harry S Truman  >>>
Collapsible: True
Length: 17 <<< - Hary S Truman >>>

Length: 72 <<<The American people have a right to know if their president is a crook. >>>
Collapsible: True
Length: 71 <<<The American people have a right to know if their president is a crok. >>>

Length: 72 <<<                                                    --- Richard Nixon   >>>
Collapsible: True
Length: 17 <<< - Richard Nixon >>>

Length: 72 <<<Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘>>>
Collapsible: False
Length: 72 <<<Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘Aะฮ‘>>>

Length: 72 <<<AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA>>>
Collapsible: True
Length:  1 <<<A>>>

REXX

<lang rexx>/*REXX program "collapses" all immediately repeated characters in a string (or strings).*/ @.= /*define a default for the @. array. */ parse arg x /*obtain optional argument from the CL.*/ if x\= then @.1= x /*if user specified an arg, use that. */

         else do;  @.1=
                   @.2= '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln '
                   @.3=  ..1111111111111111111111111111111111111111111111111111111111111117777888
                   @.4= "I never give 'em hell, I just tell the truth, and they think it's hell. "
                   @.5= '                                                   ---  Harry S Truman  '
              end
    do j=1;    L= length(@.j)                   /*obtain the length of an array element*/
    say copies('โ•', 105)                        /*show a separator line between outputs*/
    if j>1  &  L==0     then leave              /*if arg is null and  J>1, then leave. */
    new= collapse(@.j)
    say 'string' word("isn't is",1+collapsible) 'collapsible' /*display semaphore value*/
    say '    length='right(L, 3)     "   input=ยซยซยซ"  ||  @.j  ||  'ยปยปยป'
      w= length(new)
    say '    length='right(w, 3)     "  output=ยซยซยซ"  ||  new  ||  'ยปยปยป'
    end   /*j*/

exit /*stick a fork in it, we're all done. */ /*โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€*/ collapse: procedure expose collapsible; parse arg y 1 $ 2 /*get the arg; get 1st char.*/

           do k=2  to length(y)                 /*traipse through almost all the chars.*/
           _= substr(y, k, 1)                   /*pick a character from  Y  (1st arg). */
           if _==right($, 1)  then iterate      /*Is this the same character?  Skip it.*/
           $= $ || _                            /*append the character, it's different.*/
           end   /*j*/
         collapsible= y\==$;       return $     /*set boolean to  true  if collapsible.*/</lang>
output   when using the internal default inputs:
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
string isn't collapsible
    length=  0    input=ยซยซยซยปยปยป
    length=  0   output=ยซยซยซยปยปยป
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
string is collapsible
    length= 72    input=ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป
    length= 70   output=ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
string is collapsible
    length= 72    input=ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป
    length=  4   output=ยซยซยซ.178ยปยปยป
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
string is collapsible
    length= 72    input=ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป
    length= 69   output=ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
string is collapsible
    length= 72    input=ยซยซยซ                                                   ---  Harry S Truman  ยปยปยป
    length= 17   output=ยซยซยซ - Hary S Truman ยปยปยป
โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•

Ruby

This is built in since at least a decade under the method name 'squeeze'. squeeze(" ") would only squeeze spaces. <lang ruby>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",
       "๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ",]

strings.each do |str|

 puts "ยซยซยซ#{str}ยปยปยป (size #{str.size})"
 ssq = str.squeeze
 puts "ยซยซยซ#{ssq}ยปยปยป (size #{ssq.size})"
 puts

end </lang>

Output:
ยซยซยซยปยปยป (size 0)
ยซยซยซยปยปยป (size 0)

ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป (size 72)
ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป (size 70)

ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป (size 72)
ยซยซยซ.178ยปยปยป (size 4)

ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป (size 72)
ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป (size 69)

ยซยซยซ                                                   ---  Harry S Truman  ยปยปยป (size 72)
ยซยซยซ - Hary S Truman ยปยปยป (size 17)

ยซยซยซThe better the 4-wheel drive, the further you'll be from help when ya get stuck!ยปยปยป (size 80)
ยซยซยซThe beter the 4-whel drive, the further you'l be from help when ya get stuck!ยปยปยป (size 77)

ยซยซยซheadmistressshipยปยปยป (size 16)
ยซยซยซheadmistreshipยปยปยป (size 14)

ยซยซยซaardvarkยปยปยป (size 8)
ยซยซยซardvarkยปยปยป (size 7)

ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œยปยปยป (size 8)
ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ™Œยปยปยป (size 6)

Rust

<lang rust>fn collapse_string(val: &str) -> String {

   let mut output = String::new();
   let mut chars = val.chars().peekable();
   while let Some(c) = chars.next() {
       while let Some(&b) = chars.peek() {
           if b == c {
               chars.next();
           } else {
               break;
           }
       }
       output.push(c);
   }
   output

}

fn main() {

   let tests = [
       "122333444455555666666777777788888888999999999",
       "",
       "\"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",
   ];
   for s in &tests {
       println!("Old: {:>3} <<<{}>>>", s.len(), s);
       let collapsed = collapse_string(s);
       println!("New: {:>3} <<<{}>>>", collapsed.len(), collapsed);
       println!();
   }

}</lang>

Output:
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>>>

Scala

Pure imperative programming ๐Ÿ˜ƒ

<lang Scala>object CollapsibleString {

 /**Collapse a string (if possible)*/
 def collapseString (s : String) : String = {
   var res = s
   var isOver = false
   var i = 0
   if(res.size == 0) res
   else while(!isOver){
     if(res(i) == res(i+1)){
       res = res.take(i) ++ res.drop(i+1)
       i-=1
     }
     i+=1
     if(i==res.size-1) isOver = true
   }
   res
 }
 /**Check if a string is collapsible*/
 def isCollapsible (s : String) : Boolean = collapseString(s).length != s.length
 /**Display results as asked in the task*/
 def reportResults (s : String) : String = {
   val sCollapsed = collapseString(s)
   val originalRes = "ORIGINAL  : length = " + s.length() + ", string = ยซยซยซ" + s + "ยปยปยป"
   val collapsedRes = "COLLAPSED : length = " + sCollapsed.length() + ", string = ยซยซยซ" + sCollapsed + "ยปยปยป"
   //In order to avoid useless computations, the function isCollapsible isn't called
   if(s.length != sCollapsed.length) originalRes + "\n" + collapsedRes + "\n" + "This string IS collapsible !"
   else originalRes + "\n" + collapsedRes + "\n" + "This string is NOT collapsible !"
 }


 def main(args: Array[String]): Unit = {
   println(reportResults(""))
   println("------------") 
   println(reportResults("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "))
   println("------------")
   println(reportResults("..1111111111111111111111111111111111111111111111111111111111111117777888"))
   println("------------")
   println(reportResults("I never give 'em hell, I just tell the truth, and they think it's hell. "))
   println("------------")
   println(reportResults("                                                    --- Harry S Truman  "))
   println("------------")
   println(reportResults("The better the 4-wheel drive, the further you'll be from help when ya get stuck!"))
   println("------------")
   println(reportResults("headmistressship"))
   println("------------")
   println(reportResults("aardvark"))
 }


}</lang>

Output:
ORIGINAL  : length = 0, string = ยซยซยซยปยปยป
COLLAPSED : length = 0, string = ยซยซยซยปยปยป
This string is NOT collapsible !
------------
ORIGINAL  : length = 72, string = ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป
COLLAPSED : length = 70, string = ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป
This string IS collapsible !
------------
ORIGINAL  : length = 72, string = ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป
COLLAPSED : length = 4, string = ยซยซยซ.178ยปยปยป
This string IS collapsible !
------------
ORIGINAL  : length = 72, string = ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป
COLLAPSED : length = 69, string = ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป
This string IS collapsible !
------------
ORIGINAL  : length = 72, string = ยซยซยซ                                                    --- Harry S Truman  ยปยปยป
COLLAPSED : length = 17, string = ยซยซยซ - Hary S Truman ยปยปยป
This string IS collapsible !
------------
ORIGINAL  : length = 80, string = ยซยซยซThe better the 4-wheel drive, the further you'll be from help when ya get stuck!ยปยปยป
COLLAPSED : length = 77, string = ยซยซยซThe beter the 4-whel drive, the further you'l be from help when ya get stuck!ยปยปยป
This string IS collapsible !
------------
ORIGINAL  : length = 16, string = ยซยซยซheadmistressshipยปยปยป
COLLAPSED : length = 14, string = ยซยซยซheadmistreshipยปยปยป
This string IS collapsible !
------------
ORIGINAL  : length = 8, string = ยซยซยซaardvarkยปยปยป
COLLAPSED : length = 7, string = ยซยซยซardvarkยปยปยป
This string IS collapsible !

Sidef

<lang ruby>func squeeze(str) {

   str.gsub(/(.)\1+/, {|s1| s1 })

}

var 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",
       "๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ"]

strings.each {|str|

   var ssq = squeeze(str)
   say "ยซยซยซ#{str}ยปยปยป (length: #{str.len})"
   say "ยซยซยซ#{ssq}ยปยปยป (length: #{ssq.len})\n"

}</lang>

Output:
ยซยซยซยปยปยป (length: 0)
ยซยซยซยปยปยป (length: 0)

ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป (length: 72)
ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป (length: 70)

ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป (length: 72)
ยซยซยซ.178ยปยปยป (length: 4)

ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป (length: 72)
ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป (length: 69)

ยซยซยซ                                                   ---  Harry S Truman  ยปยปยป (length: 72)
ยซยซยซ - Hary S Truman ยปยปยป (length: 17)

ยซยซยซThe better the 4-wheel drive, the further you'll be from help when ya get stuck!ยปยปยป (length: 80)
ยซยซยซThe beter the 4-whel drive, the further you'l be from help when ya get stuck!ยปยปยป (length: 77)

ยซยซยซheadmistressshipยปยปยป (length: 16)
ยซยซยซheadmistreshipยปยปยป (length: 14)

ยซยซยซaardvarkยปยปยป (length: 8)
ยซยซยซardvarkยปยปยป (length: 7)

ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œยปยปยป (length: 8)
ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ™Œยปยปยป (length: 6)

Swift

<lang Swift>let 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", "๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ" ]

let collapsedStrings = strings.map { $0.replacingOccurrences( of: #"(.)\1*"#, with: "$1", options: .regularExpression)}

for (original, collapsed) in zip(strings, collapsedStrings) { print (String(format: "%03d ยซ%@ยป\n%03d ยซ%@ยป\n", original.count, original, collapsed.count, collapsed)) }</lang>

Output:
000 ยซยป
000 ยซยป

072 ยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยป
070 ยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยป

072 ยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยป
004 ยซ.178ยป

072 ยซI never give 'em hell, I just tell the truth, and they think it's hell. ยป
069 ยซI never give 'em hel, I just tel the truth, and they think it's hel. ยป

072 ยซ                                                   ---  Harry S Truman  ยป
017 ยซ - Hary S Truman ยป

080 ยซThe better the 4-wheel drive, the further you'll be from help when ya get stuck!ยป
077 ยซThe beter the 4-whel drive, the further you'l be from help when ya get stuck!ยป

016 ยซheadmistressshipยป
014 ยซheadmistreshipยป

008 ยซaardvarkยป
007 ยซardvarkยป

008 ยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œยป
006 ยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ™Œยป

Tcl

Please note the ;# ' comments, there appears to be a syntax hightlighting bug with RC <lang tcl>set test {

   {}
   {"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}

}

foreach {str} $test {

   # Uses regexp lookbehind to detect repeated characters
   set sub [regsub -all {(.)(\1+)} $str {\1}]
   # Output
   puts [format "Original (length %3d): %s" [string length $str] $str]
   puts [format "Subbed   (length %3d): %s" [string length $sub] $sub]
   puts ----------------------

} </lang>

Output:
Original (length   0):
Subbed   (length   0):
----------------------
Original (length  72): "If I were two-faced, would I be wearing this one?" --- Abraham Lincoln
Subbed   (length  70): "If I were two-faced, would I be wearing this one?" - Abraham Lincoln
----------------------
Original (length  72): ..1111111111111111111111111111111111111111111111111111111111111117777888
Subbed   (length   4): .178
----------------------
Original (length  72): I never give 'em hell, I just tell the truth, and they think it's hell.
Subbed   (length  69): I never give 'em hel, I just tel the truth, and they think it's hel.
----------------------
Original (length  72):                                                     --- Harry S Truman
Subbed   (length  17):  - Hary S Truman
----------------------
Original (length  80): The better the 4-wheel drive, the further you'll be from help when ya get stuck!
Subbed   (length  77): The beter the 4-whel drive, the further you'l be from help when ya get stuck!
----------------------
Original (length  16): headmistressship
Subbed   (length  14): headmistreship
----------------------

Visual Basic .NET

Translation of: C#

<lang vbnet>Module Module1

   Function Collapse(s As String) As String
       If String.IsNullOrEmpty(s) Then
           Return ""
       End If
       Return s(0) + New String(Enumerable.Range(1, s.Length - 1).Where(Function(i) s(i) <> s(i - 1)).Select(Function(i) s(i)).ToArray)
   End Function
   Sub Main()
       Dim input() = {
            "",
           "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
           "headmistressship",
           ControlChars.Quote + "If I were two-faced, would I be wearing this one?" + ControlChars.Quote + " --- Abraham Lincoln ",
           "..1111111111111111111111111111111111111111111111111111111111111117777888",
           "I never give 'em hell, I just tell the truth, and they think it's hell. ",
           "                                                    --- Harry S Truman  "
          }
       For Each s In input
           Console.WriteLine($"old: {s.Length} ยซยซยซ{s}ยปยปยป")
           Dim c = Collapse(s)
           Console.WriteLine($"new: {c.Length} ยซยซยซ{c}ยปยปยป")
       Next
   End Sub

End Module</lang>

Output:
old: 0 ยซยซยซยปยปยป
new: 0 ยซยซยซยปยปยป
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ยปยปยป
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 ยปยปยป

Wren

Translation of: Go
Library: Wren-fmt

<lang ecmascript>import "/fmt" for Fmt

// Returns collapsed string, original and new lengths in // unicode code points (not normalized). var collapse = Fn.new { |s|

   var c = s.codePoints.toList
   var le = c.count
   if (le < 2) return [s, le, le]
   for (i in le-2..0) {
       if (c[i] == c[i+1]) c.removeAt(i)
   }
   var cc = c.reduce("") { |acc, cp| acc + String.fromCodePoint(cp) }
   return [cc, le, cc.count]

}

var 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",
   "๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ"

]

for (s in strings) {

   var r = collapse.call(s)
   System.print("original : length = %(Fmt.d(2, r[1])), string = ยซยซยซ%(s)ยปยปยป")
   System.print("collapsed: length = %(Fmt.d(2, r[2])), string = ยซยซยซ%(r[0])ยปยปยป\n")

}</lang>

Output:
original : length =  0, string = ยซยซยซยปยปยป
collapsed: length =  0, string = ยซยซยซยปยปยป

original : length = 72, string = ยซยซยซ"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ยปยปยป
collapsed: length = 70, string = ยซยซยซ"If I were two-faced, would I be wearing this one?" - Abraham Lincoln ยปยปยป

original : length = 72, string = ยซยซยซ..1111111111111111111111111111111111111111111111111111111111111117777888ยปยปยป
collapsed: length =  4, string = ยซยซยซ.178ยปยปยป

original : length = 72, string = ยซยซยซI never give 'em hell, I just tell the truth, and they think it's hell. ยปยปยป
collapsed: length = 69, string = ยซยซยซI never give 'em hel, I just tel the truth, and they think it's hel. ยปยปยป

original : length = 72, string = ยซยซยซ                                                   ---  Harry S Truman  ยปยปยป
collapsed: length = 17, string = ยซยซยซ - Hary S Truman ยปยปยป

original : length = 80, string = ยซยซยซThe better the 4-wheel drive, the further you'll be from help when ya get stuck!ยปยปยป
collapsed: length = 77, string = ยซยซยซThe beter the 4-whel drive, the further you'l be from help when ya get stuck!ยปยปยป

original : length = 16, string = ยซยซยซheadmistressshipยปยปยป
collapsed: length = 14, string = ยซยซยซheadmistreshipยปยปยป

original : length =  8, string = ยซยซยซaardvarkยปยปยป
collapsed: length =  7, string = ยซยซยซardvarkยปยปยป

original : length =  8, string = ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œยปยปยป
collapsed: length =  6, string = ยซยซยซ๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ™Œยปยปยป

zkl

<lang zkl>fcn collapsible(str){ // no Unicode

  sink:=Sink(String);
  str.reduce('wrap(c1,c2){ if(c1!=c2) sink.write(c2); c2 },"");  // prime with \0
  cstr:=sink.close();
  return(str.len()!=cstr.len(), cstr);

}</lang> <lang zkl>strings:= 0'^ "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 American people have a right to know if their president is a crook.

                                                   --- Richard Nixon   

The better the 4-wheel drive, the further you'll be from help when ya get stuck! headmistressship aardvark^ .split("\n");

  1. <<<

foreach s in (strings){

  println("Before: %3d >>>%s<<<".fmt(s.len(),s));
  _,cstr:=collapsible(s);
  println("After:  %3d >>>%s<<<\n".fmt(cstr.len(),cstr));

}</lang>

Output:
Before:   0 >>><<<
After:    0 >>><<<

Before:  72 >>>"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln <<<
After:   70 >>>"If I were two-faced, would I be wearing this one?" - Abraham Lincoln <<<

Before:  72 >>>..1111111111111111111111111111111111111111111111111111111111111117777888<<<
After:    4 >>>.178<<<

Before:  72 >>>I never give 'em hell, I just tell the truth, and they think it's hell. <<<
After:   69 >>>I never give 'em hel, I just tel the truth, and they think it's hel. <<<

Before:  72 >>>                                                    --- Harry S Truman  <<<
After:   17 >>> - Hary S Truman <<<

Before:  72 >>>The American people have a right to know if their president is a crook. <<<
After:   71 >>>The American people have a right to know if their president is a crok. <<<

Before:  72 >>>                                                    --- Richard Nixon   <<<
After:   17 >>> - Richard Nixon <<<

Before:  80 >>>The better the 4-wheel drive, the further you'll be from help when ya get stuck!<<<
After:   77 >>>The beter the 4-whel drive, the further you'l be from help when ya get stuck!<<<

Before:  16 >>>headmistressship<<<
After:   14 >>>headmistreship<<<

Before:   8 >>>aardvark<<<
After:    7 >>>ardvark<<<