Determine if a string is squeezable: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 1,445: Line 1,445:
ans = ║ --- Hary S Truman ║
ans = ║ --- Hary S Truman ║
</pre>
</pre>

=={{header|Nim}}==
<lang Nim>import unicode, strformat

proc squeeze(s: string; ch: Rune) =
echo fmt"Specified character: '{ch}'"
let original = s.toRunes
echo fmt"Original: length = {original.len}, string = «««{s}»»»"
var previous = Rune(0)
var squeezed: seq[Rune]
for rune in original:
if rune != previous:
squeezed.add(rune)
previous = rune
elif rune != ch:
squeezed.add(rune)
echo fmt"Squeezed: length = {squeezed.len}, string = «««{squeezed}»»»"
echo ""


const 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",
"😍😀🙌💃😍😍😍🙌",]

const Chars = [@[Rune(' ')], @[Rune('-')], @[Rune('7')], @[Rune('.')],
@[Rune(' '), Rune('-'), Rune('r')],
@[Rune('e')], @[Rune('s')], @[Rune('a')], "😍".toRunes]


for i, s in Strings:
for ch in Chars[i]:
s.squeeze(ch)</lang>

{{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|Perl}}==
=={{header|Perl}}==

Revision as of 22:41, 19 November 2020

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

Determine if a character string is   squeezable.

And if so,   squeeze the string   (by removing any number of a   specified   immediately repeated   character).


This task is very similar to the task     Determine if a character string is collapsible     except that only a specified character is   squeezed   instead of any character that is immediately repeated.


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


A specified   immediately repeated   character is any specified 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:   squeeze.}


Examples

In the following character string with a specified   immediately repeated   character of   e:


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


Only the 2nd   e   is an specified repeated character,   indicated by an underscore (above),   even though they (the characters) appear elsewhere in the character string.


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

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



Another example: In the following character string,   using a specified immediately repeated character   s:

 headmistressship 


The "squeezed" string would be:

 headmistreship 


Task

Write a subroutine/function/procedure/routine···   to locate a   specified immediately repeated   character and   squeeze   (delete)   them from the character string.   The character string can be processed from either direction.


Show all output here, on this page:

  •   the   specified repeated character   (to be searched for and possibly squeezed):
  •   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:

                                                                                  immediately
 string                                                                            repeated
 number                                                                            character
                                                                                     ( ↓   a blank,  a minus,  a seven,  a period)
        ╔╗
   1    ║╚═══════════════════════════════════════════════════════════════════════╗    ' '    ◄■■■■■■  a null string  (length zero)
   2    ║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║    '-'
   3    ║..1111111111111111111111111111111111111111111111111111111111111117777888║    '7'
   4    ║I never give 'em hell, I just tell the truth, and they think it's hell. ║    '.'
   5    ║                                                    --- Harry S Truman  ║  (below)  ◄■■■■■■  has many repeated blanks
        ╚════════════════════════════════════════════════════════════════════════╝     ↑
                                                                                       │
                                                                                       │
        For the 5th string  (Truman's signature line),  use each of these  specified immediately  repeated characters:
                                  •  a blank
                                  •  a minus
                                  •  a lowercase  r


Note:   there should be seven results shown,   one each for the 1st four strings,   and three results for the 5th string.

Other tasks related to string operations:
Metrics
Counting
Remove/replace
Anagrams/Derangements/shuffling
Find/Search/Determine
Formatting
Song lyrics/poems/Mad Libs/phrases
Tokenize
Sequences



Ada

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

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

begin

  Squeeze ("", ' ');
  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. ", '.');
  Squeeze ("                                                    --- Harry S Truman  ", ' ');
  Squeeze ("                                                    --- Harry S Truman  ", '-');
  Squeeze ("                                                    --- Harry S Truman  ", 'r');

end Test_Squeezable; </lang>

Output:
Character to be squeezed: ' '
Input  = <<<>>>, length = 0
Output = <<<>>>, length = 0
Character to be squeezed: '-'
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
Character to be squeezed: '7'
Input  = <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length = 72
Output = <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>, length = 69
Character to be squeezed: '.'
Input  = <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72
Output = <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>, length = 72
Character to be squeezed: ' '
Input  = <<<                                                    --- Harry S Truman  >>>, length = 72
Output = <<< --- Harry S Truman >>>, length = 20
Character to be squeezed: '-'
Input  = <<<                                                    --- Harry S Truman  >>>, length = 72
Output = <<<                                                    - Harry S Truman  >>>, length = 70
Character to be squeezed: 'r'
Input  = <<<                                                    --- Harry S Truman  >>>, length = 72
Output = <<<                                                    --- Hary S Truman  >>>, length = 71

ALGOL 68

<lang algol68>BEGIN

   # returns a squeezed version of s                                     #
   #         i.e. s with adjacent duplicate c characters removed         #
   PRIO SQUEEZE = 9;
   OP   SQUEEZE = ( STRING s, CHAR c )STRING:
        IF s = ""
        THEN ""   # empty string                                         #
        ELSE      # non-empty string                                     #
           [ LWB s : UPB s ]CHAR result;
           INT     r pos   := LWB result;
           result[ r pos ] := s[ LWB s ];
           FOR s pos FROM LWB s + 1 TO UPB s DO
               IF  result[ r pos ] /= s[ s pos ]
               OR  result[ r pos ] /= c
               THEN
                   r pos +:= 1;
                   result[ r pos ] := s[ s pos ]
               FI
           OD;
           result[ LWB result : r pos ]
        FI # SQUEEZE # ;
   # test the SQUEEZE operator                                           #
   PROC test squeeze = ( STRING s, CHAR c )VOID:
       BEGIN
           STRING z = s SQUEEZE c;
           print( ( "Squeezing """, c, """ in ", "<<<", s, ">>> (length ", whole( ( UPB s + 1 ) - LWB s, 0 ), ")", newline ) );
           print( ( "       ->        ",         "<<<", z, ">>> (length ", whole( ( UPB z + 1 ) - LWB z, 0 ), ")", newline ) )
       END # test squeeze # ;
   # task test cases                                                     #
   test squeeze( "",                                                                           " " );
   test squeeze( """If I were two-faced, would I be wearing this one?"" --- Abraham Lincoln ", "-" );
   test squeeze( "..1111111111111111111111111111111111111111111111111111111111111117777888",   "7" );
   test squeeze( "I never give 'em hell, I just tell the truth, and they think it's hell. ",   "." );
   STRING hst =  "                                                    --- Harry S Truman  ";
   test squeeze( hst, " " );
   test squeeze( hst, "-" );
   test squeeze( hst, "r" )

END</lang>

Output:
Squeezing " " in <<<>>> (length 0)
       ->        <<<>>> (length 0)
Squeezing "-" in <<<"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)
Squeezing "7" in <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (length 72)
       ->        <<<..1111111111111111111111111111111111111111111111111111111111111117888>>> (length 69)
Squeezing "." in <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (length 72)
       ->        <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (length 72)
Squeezing " " in <<<                                                    --- Harry S Truman  >>> (length 72)
       ->        <<< --- Harry S Truman >>> (length 20)
Squeezing "-" in <<<                                                    --- Harry S Truman  >>> (length 72)
       ->        <<<                                                    - Harry S Truman  >>> (length 70)
Squeezing "r" in <<<                                                    --- Harry S Truman  >>> (length 72)
       ->        <<<                                                    --- Hary S Truman  >>> (length 71)

AutoHotkey

<lang AutoHotkey>squeezable_string(str, char){

   for i, ch in StrSplit(str){
       if (ch <> prev) || !InStr(ch, char)
           res .= ch
       prev := ch
   }
   result := "
   (ltrim
   Original string:`t" StrLen(str) " characters`t«««" str "»»»
   Squeezable Character «««" char "»»»
   Resultant string:`t" StrLen(res) " characters`t«««" res "»»»
   )"
   return result

}</lang> Examples:<lang AutoHotkey>data := [""

   , """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. "]

char := ["","-","7","."] for i, str in data

   MsgBox % squeezable_string(str, char[i])

str := " --- Harry S Truman " for i, char in [" ","-","r"]

   MsgBox % squeezable_string(str, char)

return</lang>

Outputs:

---------------------------
Original string:    0 characters    «««»»»
Squeezable Character «««»»»
Resultant string:   0 characters    «««»»»
---------------------------
Original string:    72 characters   «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
Squeezable Character «««-»»»
Resultant string:   70 characters   «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»
---------------------------
Original string:    72 characters   «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
Squeezable Character «««7»»»
Resultant string:   69 characters   «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
---------------------------
Original string:    72 characters   «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
Squeezable Character «««.»»»
Resultant string:   72 characters   «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
---------------------------
Original string:    72 characters   «««                                                    --- Harry S Truman  »»»
Squeezable Character ««« »»»
Resultant string:   20 characters   ««« --- Harry S Truman »»»
---------------------------
Original string:    72 characters   «««                                                    --- Harry S Truman  »»»
Squeezable Character «««-»»»
Resultant string:   70 characters   «««                                                    - Harry S Truman  »»»
---------------------------
Original string:    72 characters   «««                                                    --- Harry S Truman  »»»
Squeezable Character «««r»»»
Resultant string:   71 characters   «««                                                    --- Hary S Truman  »»»
---------------------------

AWK

<lang AWK>

  1. syntax: GAWK -f DETERMINE_IF_A_STRING_IS_SQUEEZABLE.AWK

BEGIN {

   arr[++n] = ""                                                                                 ; arr2[n] = " "
   arr[++n] = "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "       ; arr2[n] = "-"
   arr[++n] = "..1111111111111111111111111111111111111111111111111111111111111117777888"         ; arr2[n] = "7"
   arr[++n] = "I never give 'em hell, I just tell the truth, and they think it's hell. "         ; arr2[n] = "."
   arr[++n] = "                                                    --- Harry S Truman  "         ; arr2[n] = " -r"
   arr[++n] = "The better the 4-wheel drive, the further you'll be from help when ya get stuck!" ; arr2[n] = "e"
   arr[++n] = "headmistressship"                                                                 ; arr2[n] = "s"
   for (i=1; i<=n; i++) {
     for (j=1; j<=length(arr2[i]); j++) {
       main(arr[i],substr(arr2[i],j,1))
     }
   }
   exit(0)

} function main(str,chr, c,i,new_str,prev_c) {

   for (i=1; i<=length(str); i++) {
     c = substr(str,i,1)
     if (!(prev_c == c && c == chr)) {
       prev_c = c
       new_str = new_str c
     }
   }
   printf("use: '%s'\n",chr)
   printf("old: %2d <<<%s>>>\n",length(str),str)
   printf("new: %2d <<<%s>>>\n\n",length(new_str),new_str)

} </lang>

Output:
use: ' '
old:  0 <<<>>>
new:  0 <<<>>>

use: '-'
old: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
new: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>

use: '7'
old: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
new: 69 <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>

use: '.'
old: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
new: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>

use: ' '
old: 72 <<<                                                    --- Harry S Truman  >>>
new: 20 <<< --- Harry S Truman >>>

use: '-'
old: 72 <<<                                                    --- Harry S Truman  >>>
new: 70 <<<                                                    - Harry S Truman  >>>

use: 'r'
old: 72 <<<                                                    --- Harry S Truman  >>>
new: 71 <<<                                                    --- Hary S Truman  >>>

use: 'e'
old: 80 <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>>
new: 79 <<<The better the 4-whel drive, the further you'll be from help when ya get stuck!>>>

use: 's'
old: 16 <<<headmistressship>>>
new: 14 <<<headmistreship>>>

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. <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[100],char str2[100]){

   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 squeeze "The better the 4-wheel drive, the further you'll be from help when ya get stuck!" t
Specified Operation : SQUEEZE
Target Character : t
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-wheel drive, the further you'll be from help when ya get stuck!»»»
Length : 79

C:\My Projects\networks>a squeeze "The better the 4-wheel drive, the further you'll be from help when ya get stuck!" e
Specified Operation : SQUEEZE
Target Character : e
Original «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»»
Length : 80
Final    «««The better the 4-whel drive, the further you'll be from help when ya get stuck!»»»
Length : 79

C:\My Projects\networks>a squeeze "The better the 4-wheel drive, the further you'll be from help when ya get stuck!" l
Specified Operation : SQUEEZE
Target Character : l
Original «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»»
Length : 80
Final    «««The better the 4-wheel drive, the further you'l be from help when ya get stuck!»»»
Length : 79

C:\My Projects\networks>a squeeze headmistressship s
Specified Operation : SQUEEZE
Target Character : s
Original «««headmistressship»»»
Length : 16
Final    «««headmistreship»»»
Length : 14

C:\My Projects\networks>a squeeze "" ""
Specified Operation : SQUEEZE
Target Character :
Original «««»»»
Length : 0
Final    «««»»»
Length : 0

C:\My Projects\networks>a squeeze "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln" -
Specified Operation : SQUEEZE
Target Character : -
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 squeeze ..1111111111111111111111111111111111111111111111111111111111111117777888 7
Specified Operation : SQUEEZE
Target Character : 7
Original «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
Length : 72
Final    «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
Length : 69

C:\My Projects\networks>a squeeze "I never give 'em hell, I just tell the truth, and they think it's hell." .
Specified Operation : SQUEEZE
Target Character : .
Original «««I never give 'em hell, I just tell the truth, and they think it's hell.»»»
Length : 71
Final    «««I never give 'em hell, I just tell the truth, and they think it's hell.»»»
Length : 71

C:\My Projects\networks>a squeeze "                                                   ---  Harry S Truman  " " "
Specified Operation : SQUEEZE
Target Character :
Original «««                                                   ---  Harry S Truman  »»»
Length : 72
Final    ««« --- Harry S Truman »»»
Length : 20

C:\My Projects\networks>a squeeze "                                                   ---  Harry S Truman  " -
Specified Operation : SQUEEZE
Target Character : -
Original «««                                                   ---  Harry S Truman  »»»
Length : 72
Final    «««                                                   -  Harry S Truman  »»»
Length : 70

C:\My Projects\networks>a squeeze "                                                   ---  Harry S Truman  " r
Specified Operation : SQUEEZE
Target Character : r
Original «««                                                   ---  Harry S Truman  »»»
Length : 72
Final    «««                                                   ---  Hary S Truman  »»»
Length : 71

C#

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

public class Program {

   static void Main()
   {
       SqueezeAndPrint("", ' ');
       SqueezeAndPrint("\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln ", '-');
       SqueezeAndPrint("..1111111111111111111111111111111111111111111111111111111111111117777888", '7');
       SqueezeAndPrint("I never give 'em hell, I just tell the truth, and they think it's hell. ", '.');
       string s = "                                                    --- Harry S Truman  ";
       SqueezeAndPrint(s, ' ');
       SqueezeAndPrint(s, '-');
       SqueezeAndPrint(s, 'r');
   }
   static void SqueezeAndPrint(string s, char c) {
       Console.WriteLine($"squeeze: '{c}'");
       Console.WriteLine($"old: {s.Length} «««{s}»»»");
       s = Squeeze(s, c);
       Console.WriteLine($"new: {s.Length} «««{s}»»»");
   }
   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());

}</lang>

Output:
squeeze: ' '
old: 0 «««»»»
new: 0 «««»»»
squeeze: '-'
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 »»»
squeeze: '7'
old: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
new: 69 «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
squeeze: '.'
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. »»»
squeeze: ' '
old: 72 «««                                                    --- Harry S Truman  »»»
new: 20 ««« --- Harry S Truman »»»
squeeze: '-'
old: 72 «««                                                    --- Harry S Truman  »»»
new: 70 «««                                                    - Harry S Truman  »»»
squeeze: 'r'
old: 72 «««                                                    --- Harry S Truman  »»»
new: 71 «««                                                    --- Hary S Truman  »»»

C++

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

  1. include <string>
  2. include <iostream>

template<typename char_type> std::basic_string<char_type> squeeze(std::basic_string<char_type> str, char_type ch) {

   auto i = std::unique(str.begin(), str.end(),
       [ch](char_type a, char_type b) { return a == ch && b == ch; });
   str.erase(i, str.end());
   return str;

}

void test(const std::string& str, char ch) {

   std::cout << "character: '" << ch << "'\n";
   std::cout << "original: <<<" << str << ">>>, length: " << str.length() << '\n';
   std::string squeezed(squeeze(str, ch));
   std::cout << "result: <<<" << squeezed << ">>>, length: " << squeezed.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", '7');
   test("I never give 'em hell, I just tell the truth, and they think it's hell. ", '.');
   std::string truman("                                                    --- Harry S Truman  ");
   test(truman, ' ');
   test(truman, '-');
   test(truman, 'r');
   return 0;

}</lang>

Output:
character: ' '
original: <<<>>>, length: 0
result: <<<>>>, length: 0

character: '-'
original: <<<"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

character: '7'
original: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length: 72
result: <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>, length: 69

character: '.'
original: <<<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

character: ' '
original: <<<                                                    --- Harry S Truman  >>>, length: 72
result: <<< --- Harry S Truman >>>, length: 20

character: '-'
original: <<<                                                    --- Harry S Truman  >>>, length: 72
result: <<<                                                    - Harry S Truman  >>>, length: 70

character: 'r'
original: <<<                                                    --- Harry S Truman  >>>, length: 72
result: <<<                                                    --- Hary S Truman  >>>, length: 71

Clojure

<lang Clojure> (defn squeeze [s c]

 (let [spans (partition-by #(= c %) s)
       span-out (fn [span]
                  (if (= c (first span))
                    (str c)
                    (apply str span)))]
   (apply str (map span-out spans))))

(defn test-squeeze [s c]

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

</lang>

Output:
(def test-data [""
                "\"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  "])

(test-squeeze (nth test-data 0) (first " "))
  Input:   <<<>>> (len 0)
  becomes: <<<>>> (len 0)
(test-squeeze (nth test-data 1) \-)
  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)
(test-squeeze (nth test-data 2) \7)
  Input:   <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (len 72)
  becomes: <<<..1111111111111111111111111111111111111111111111111111111111111117888>>> (len 69)
(test-squeeze (nth test-data 3) \.)
  Input:   <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (len 72)
  becomes: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (len 72)
(test-squeeze (nth test-data 4) (first " "))
  Input:   <<<                                                    --- Harry S Truman  >>> (len 72)
  becomes: <<< --- Harry S Truman >>> (len 20)
(test-squeeze (nth test-data 4) \-)
  Input:   <<<                                                    --- Harry S Truman  >>> (len 72)
  becomes: <<<                                                    - Harry S Truman  >>> (len 70)
(test-squeeze (nth test-data 4) \r)
  Input:   <<<                                                    --- Harry S Truman  >>> (len 72)
  becomes: <<<                                                    --- Hary S Truman  >>> (len 71)

D

Translation of: C#

<lang d>import std.stdio;

void squeezable(string s, char rune) {

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

}

void main() {

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

}</lang>

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

squeeze: '-'
old: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>, length = 72
new: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>, length = 70

squeeze: '7'
old: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length = 72
new: <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>, length = 69

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

squeeze: ' '
old: <<<                                                    --- Harry S Truman  >>>, length = 72
new: <<< --- Harry S Truman >>>, length = 20

squeeze: '-'
old: <<<                                                    --- Harry S Truman  >>>, length = 72
new: <<<                                                    - Harry S Truman  >>>, length = 70

squeeze: 'r'
old: <<<                                                    --- Harry S Truman  >>>, length = 72
new: <<<                                                    --- Hary S Truman  >>>, length = 71

F#

<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

               let fN=let mutable g=n.[0] in (fun n->if n=i && n=g then false else g<-n; true)
               let fG=n.[0..0]+System.String(n.[1..].ToCharArray()|>Array.filter fN)
               if fG.Length=n.Length then None else Some fG

let isSqueezable n g=match squeeze n g with

                     Some i->printfn "%A squeezes <<<%s>>> (length %d) to <<<%s>>> (length %d)" g n n.Length i i.Length
                    |_->printfn "%A does not squeeze <<<%s>>> (length %d)" g n n.Length

isSqueezable "" ' ' isSqueezable "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln " '-' isSqueezable "..1111111111111111111111111111111111111111111111111111111111111117777888" '7' 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' </lang>

Output:
' ' does not squeeze <<<>>> (length 0)
'-' squeezes <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> (length 72) to <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> (length 70)
'7' squeezes <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (length 72) to <<<..1111111111111111111111111111111111111111111111111111111111111117888>>> (length 69)
'.' does not squeeze <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (length 72)
' ' squeezes <<<                                                    --- Harry S Truman  >>> (length 72) to <<< --- Harry S Truman >>> (length 20)
'-' squeezes <<<                                                    --- Harry S Truman  >>> (length 72) to <<<                                                    - Harry S Truman  >>> (length 70)
'r' squeezes <<<                                                    --- Harry S Truman  >>> (length 72) to <<<                                                    --- Hary S Truman  >>> (length 71)

Factor

<lang factor>USING: formatting fry io kernel math sbufs sequences strings ; IN: rosetta-code.squeeze

(squeeze) ( str c -- new-str )
   [ unclip-slice 1string >sbuf ] dip
   '[ over last over [ _ = ] both? [ drop ] [ suffix! ] if ]
   reduce >string ;
squeeze ( str c -- new-str )
   over empty? [ 2drop "" ] [ (squeeze) ] if ;
.str ( str -- ) dup length "«««%s»»» (length %d)\n" printf ;
show-squeeze ( str c -- )
   dup "Specified character: '%c'\n" printf
   [ "Before squeeze: " write drop .str ]
   [ "After  squeeze: " write squeeze .str ] 2bi nl ;
squeeze-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. "
   } "\0-7." [ show-squeeze ] 2each
   "                                                   ---  Harry S Truman  "
   [ CHAR: space ] [ CHAR: - ] [ CHAR: r ] tri
   [ show-squeeze ] 2tri@ ;

MAIN: squeeze-demo</lang>

Output:
Specified character: ' '
Before squeeze: «««»»» (length 0)
After  squeeze: «««»»» (length 0)

Specified character: '-'
Before squeeze: «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» (length 72)
After  squeeze: «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» (length 70)

Specified character: '7'
Before squeeze: «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» (length 72)
After  squeeze: «««..1111111111111111111111111111111111111111111111111111111111111117888»»» (length 69)

Specified character: '.'
Before squeeze: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length 72)
After  squeeze: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length 72)

Specified character: ' '
Before squeeze: «««                                                   ---  Harry S Truman  »»» (length 72)
After  squeeze: ««« --- Harry S Truman »»» (length 20)

Specified character: '-'
Before squeeze: «««                                                   ---  Harry S Truman  »»» (length 72)
After  squeeze: «««                                                   -  Harry S Truman  »»» (length 70)

Specified character: 'r'
Before squeeze: «««                                                   ---  Harry S Truman  »»» (length 72)
After  squeeze: «««                                                   ---  Hary S Truman  »»» (length 71)

Go

<lang go>package main

import "fmt"

// Returns squeezed string, original and new lengths in // unicode code points (not normalized). func squeeze(s string, c rune) (string, int, int) {

   r := []rune(s)
   le, del := len(r), 0
   for i := le - 2; i >= 0; i-- {
       if r[i] == c && 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",
       "😍😀🙌💃😍😍😍🙌",
   }
   chars := [][]rune{{' '}, {'-'}, {'7'}, {'.'}, {' ', '-', 'r'}, {'e'}, {'s'}, {'a'}, {'😍'}}
   for i, s := range strings {
       for _, c := range chars[i] {
           ss, olen, slen := squeeze(s, c)
           fmt.Printf("specified character = %q\n", c)
           fmt.Printf("original : length = %2d, string = «««%s»»»\n", olen, s)
           fmt.Printf("squeezed : length = %2d, string = «««%s»»»\n\n", slen, ss)
       }
   }

}</lang>

Output:
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 = «««😍😀🙌💃😍🙌»»»

Haskell

<lang haskell>import Text.Printf (printf)

input :: [(String, Char)] input = [ ("", ' ')

       , ("The better the 4-wheel drive, the further you'll be from help when ya get stuck!", 'e')
       , ("headmistressship", 's')
       , ("\"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')
       , ("aardvark", 'a')
       , ("😍😀🙌💃😍😍😍🙌", '😍')
       ]

collapse :: Eq a => [a] -> a -> [a] collapse s c = go s

 where go [] = []
       go (x:y:xs)
         | x == y && x == c = go (y:xs)
         | otherwise        = x : go (y:xs)
       go xs = xs

main :: IO () 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</lang>
Output:
squeeze: ' '
old:   0 «««»»»
new:   0 «««»»»

squeeze: '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!»»»

squeeze: 's'
old:  16 «««headmistressship»»»
new:  14 «««headmistreship»»»

squeeze: '-'
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 »»»

squeeze: '7'
old:  72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
new:  69 «««..1111111111111111111111111111111111111111111111111111111111111117888»»»

squeeze: '.'
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. »»»

squeeze: 'r'
old:  72 «««                                                    --- Harry S Truman  »»»
new:  71 «««                                                    --- Hary S Truman  »»»

squeeze: 'a'
old:   8 «««aardvark»»»
new:   7 «««ardvark»»»

squeeze: '😍'
old:   8 «««😍😀🙌💃😍😍😍🙌»»»
new:   6 «««😍😀🙌💃😍🙌»»»

Java

<lang Java>

// Title: Determine if a string is squeezable

public class StringSqueezable {

   public static void main(String[] args) {
       String[] testStrings = 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[] testChar = new String[] {
               " ", 
               "-", 
               "7", 
               ".", 
               " -r",
               "5",
               "e",
               "s"};
       for ( int testNum = 0 ; testNum < testStrings.length ; testNum++ ) {
           String s = testStrings[testNum];
           for ( char c : testChar[testNum].toCharArray() ) {
               String result = squeeze(s, c);
               System.out.printf("use: '%c'%nold:  %2d <<<%s>>>%nnew:  %2d <<<%s>>>%n%n", c, s.length(), s, result.length(), result);
           }
       }
   }
   
   private static String squeeze(String in, char include) {
       StringBuilder sb = new StringBuilder();
       for ( int i = 0 ; i < in.length() ; i++ ) {
           if ( i == 0 || in.charAt(i-1) != in.charAt(i) || (in.charAt(i-1) == in.charAt(i) && in.charAt(i) != include)) {
               sb.append(in.charAt(i));
           }
       }
       return sb.toString();
   }

} </lang>

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

Julia

<lang julia>const teststringpairs = [

   ("", ' '),
   (""""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  ", ' '),
   ("                                                    --- Harry S Truman  ", '-'),
   ("                                                    --- Harry S Truman  ", 'r')]

function squeezed(s, c)

   t = isempty(s) ? "" : s[1:1]
   for x in s[2:end]
       if x != t[end] || x != c
           t *= x
       end
   end
   t

end

for (s, c) in teststringpairs

   n, t = length(s), squeezed(s, c)
   println("«««$s»»» (length $n)\n",
       s == t ? "is not squeezed, so remains" : "squeezes to", 
       ":\n«««$t»»» (length $(length(t))).\n")

end

</lang>

Output:
«««»»» (length 0)
is not squeezed, so remains:
«««»»» (length 0).

«««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» (length 72)
squeezes to:
«««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» (length 70).

«««..1111111111111111111111111111111111111111111111111111111111111117777888»»» (length 72)
squeezes to:
«««..1111111111111111111111111111111111111111111111111111111111111117888»»» (length 69).

«««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length 72)
is not squeezed, so remains:
«««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length 72).

«««                                                    --- Harry S Truman  »»» (length 72)
squeezes to:
««« --- Harry S Truman »»» (length 20).

«««                                                    --- Harry S Truman  »»» (length 72)
squeezes to:
«««                                                    - Harry S Truman  »»» (length 70).

«««                                                    --- Harry S Truman  »»» (length 72)
squeezes to:
«««                                                    --- Hary S Truman  »»» (length 71).

Kotlin

Translation of: Java

<lang scala>fun main() {

   val testStrings = 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  ",
       "122333444455555666666777777788888888999999999",
       "The better the 4-wheel drive, the further you'll be from help when ya get stuck!",
       "headmistressship")
   val testChar = arrayOf(
       " ",
       "-",
       "7",
       ".",
       " -r",
       "5",
       "e",
       "s")
   for (testNum in testStrings.indices) {
       val s = testStrings[testNum]
       for (c in testChar[testNum].toCharArray()) {
           val result = squeeze(s, c)
           System.out.printf("use: '%c'%nold:  %2d >>>%s<<<%nnew:  %2d >>>%s<<<%n%n", c, s.length, s, result.length, result)
       }
   }

}

private fun squeeze(input: String, include: Char): String {

   val sb = StringBuilder()
   for (i in input.indices) {
       if (i == 0 || input[i - 1] != input[i] || input[i - 1] == input[i] && input[i] != include) {
           sb.append(input[i])
       }
   }
   return sb.toString()

}</lang>

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

Lua

Translation of: D

<lang lua>function squeezable(s, rune)

   print("squeeze: `" .. rune .. "`")
   print("old: <<<" .. s .. ">>>, length = " .. string.len(s))
   local last = nil
   local le = 0
   io.write("new: <<<")
   for c in s:gmatch"." do
       if c ~= last or c ~= rune then
           io.write(c)
           le = le + 1
       end
       last = c
   end
   print(">>>, length = " .. le)
   print()

end

function main()

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

   local s = "                                                    --- Harry S Truman  "
   squeezable(s, ' ')
   squeezable(s, '-')
   squeezable(s, 'r')

end

main()</lang>

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

squeeze: `-`
old: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>, length = 72
new: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>, length = 70

squeeze: `7`
old: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>, length = 72
new: <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>, length = 69

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

squeeze: ` `
old: <<<                                                    --- Harry S Truman  >>>, length = 72
new: <<< --- Harry S Truman >>>, length = 20

squeeze: `-`
old: <<<                                                    --- Harry S Truman  >>>, length = 72
new: <<<                                                    - Harry S Truman  >>>, length = 70

squeeze: `r`
old: <<<                                                    --- Harry S Truman  >>>, length = 72
new: <<<                                                    --- Hary S Truman  >>>, length = 71


MATLAB / Octave

squeeze is a base function Matlab and Octave, therefore its name is changed to squeezee()

<lang Matlab> function r = squeezee(s,c)

 ix = [];
 c  = unique(c);
 for k=1:length(c)
   ix=[ix; find((s(1:end-1)==s(2:end)) & (s(1:end-1)==c(k)))+1];
 end
 r=s;
 r(ix)=[];     
 fprintf(1,'Character to be squeezed: "%s"\n',c);
 fprintf(1,'Input:  <<<%s>>>  length: %d\n',s,length(s));
 fprintf(1,'Output: <<<%s>>>  length: %d\n',r,length(r));
 fprintf(1,'Character to be squeezed: "%s"\n',c);

end


squeezee(, ' ') squeezee('║╚═══════════════════════════════════════════════════════════════════════╗', '-') squeezee('║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║', '7') squeezee('║..1111111111111111111111111111111111111111111111111111111111111117777888║', '.') squeezee('║I never give em hell, I just tell the truth, and they think its hell. ║', '.') squeezee('║ --- Harry S Truman ║', '.') squeezee('║ --- Harry S Truman ║', '-') squeezee('║ --- Harry S Truman ║', 'r')

</lang>

Output:
octave:1> test squeezee
Character to be squeezed: " "
Input:  <<<>>>  length: 0
Output: <<<>>>  length: 0
Character to be squeezed: " "
ans =
Character to be squeezed: "-"
Input:  <<<║╚═══════════════════════════════════════════════════════════════════════╗>>>  length: 222
Output: <<<║╚═══════════════════════════════════════════════════════════════════════╗>>>  length: 222
Character to be squeezed: "-"
ans = ║╚═══════════════════════════════════════════════════════════════════════╗
Character to be squeezed: "7"
Input:  <<<║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║>>>  length: 78
Output: <<<║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║>>>  length: 78
Character to be squeezed: "7"
ans = ║"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln ║
Character to be squeezed: "."
Input:  <<<║..1111111111111111111111111111111111111111111111111111111111111117777888║>>>  length: 78
Output: <<<║.1111111111111111111111111111111111111111111111111111111111111117777888║>>>  length: 77
Character to be squeezed: "."
ans = ║.1111111111111111111111111111111111111111111111111111111111111117777888║
Character to be squeezed: "."
Input:  <<<║I never give 'em hell, I just tell the truth, and they think it's hell. ║>>>  length: 78
Output: <<<║I never give 'em hell, I just tell the truth, and they think it's hell. ║>>>  length: 78
Character to be squeezed: "."
ans = ║I never give 'em hell, I just tell the truth, and they think it's hell. ║
Character to be squeezed: "."
Input:  <<<║                                                    --- Harry S Truman  ║>>>  length: 78
Output: <<<║                                                    --- Harry S Truman  ║>>>  length: 78
Character to be squeezed: "."
ans = ║                                                    --- Harry S Truman  ║
Character to be squeezed: "-"
Input:  <<<║                                                    --- Harry S Truman  ║>>>  length: 78
Output: <<<║                                                    - Harry S Truman  ║>>>  length: 76
Character to be squeezed: "-"
ans = ║                                                    - Harry S Truman  ║
Character to be squeezed: "r"
Input:  <<<║                                                    --- Harry S Truman  ║>>>  length: 78
Output: <<<║                                                    --- Hary S Truman  ║>>>  length: 77
Character to be squeezed: "r"
ans = ║                                                    --- Hary S Truman  ║

Nim

<lang Nim>import unicode, strformat

proc squeeze(s: string; ch: Rune) =

 echo fmt"Specified character: '{ch}'"
 let original = s.toRunes
 echo fmt"Original: length = {original.len}, string = «««{s}»»»"
 var previous = Rune(0)
 var squeezed: seq[Rune]
 for rune in original:
   if rune != previous:
     squeezed.add(rune)
     previous = rune
   elif rune != ch:
     squeezed.add(rune)
 echo fmt"Squeezed: length = {squeezed.len}, string = «««{squeezed}»»»"
 echo ""


const 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",
       "😍😀🙌💃😍😍😍🙌",]

const Chars = [@[Rune(' ')], @[Rune('-')], @[Rune('7')], @[Rune('.')],

              @[Rune(' '), Rune('-'), Rune('r')],
              @[Rune('e')], @[Rune('s')], @[Rune('a')], "😍".toRunes]


for i, s in Strings:

 for ch in Chars[i]:
   s.squeeze(ch)</lang>
Output:
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 = «««😍😀🙌💃😍🙌»»»

Perl

<lang perl>use strict; use warnings; use Unicode::UCD 'charinfo';

for (

 [, ' '],
 ['"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  ', ' '],
 ['                                                    --- Harry S Truman  ', '-'],
 ['                                                    --- Harry S Truman  ', 'r']

) {

   my($phrase,$char) = @$_;
   (my $squeeze = $phrase) =~ s/([$char])\1+/$1/g;
   printf "\nOriginal length: %d <<<%s>>>\nSqueezable on \"%s\": %s\nSqueezed length: %d <<<%s>>>\n",
       length($phrase), $phrase,
       charinfo(ord $char)->{'name'},
       $phrase ne $squeeze ? 'True' : 'False',
       length($squeeze), $squeeze

}</lang>

Output:
Original length: 0 <<<>>>
Squeezable on "SPACE": False
Squeezed length: 0 <<<>>>

Original length: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
Squeezable on "HYPHEN-MINUS": True
Squeezed length: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>

Original length: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
Squeezable on "DIGIT SEVEN": True
Squeezed length: 69 <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>

Original length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
Squeezable on "FULL STOP": False
Squeezed length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>

Original length: 72 <<<                                                    --- Harry S Truman  >>>
Squeezable on "SPACE": True
Squeezed length: 20 <<< --- Harry S Truman >>>

Original length: 72 <<<                                                    --- Harry S Truman  >>>
Squeezable on "HYPHEN-MINUS": True
Squeezed length: 70 <<<                                                    - Harry S Truman  >>>

Original length: 72 <<<                                                    --- Harry S Truman  >>>
Squeezable on "LATIN SMALL LETTER R": True
Squeezed length: 71 <<<                                                    --- Hary S Truman  >>>

Phix

<lang Phix>constant tests = {"", " ",

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

length %2d input: <<<%s>>> length %2d squeeze(%c): <<<%s>>> """ function squeeze(sequence s, integer ch)

   if length(s)>1 then
       integer outdx = 1
       object prev = s[1]
       for i=2 to length(s) do
           if s[i]!=prev or prev!=ch then
               prev = s[i]
               outdx += 1
               s[outdx] = prev
           end if
       end for
       s = s[1..outdx]
   end if
   return s

end function

for i=1 to length(tests) by 2 do

   string ti = tests[i], chars = tests[i+1]
   for j=1 to length(chars) do
       string si = squeeze(ti,chars[j])
       printf(1,fmt,{length(ti),ti,length(si),chars[j],si})
   end for

end for</lang>

Output:
length  0      input: <<<>>>
length  0 squeeze( ): <<<>>>

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

length 72      input: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
length 69 squeeze(7): <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>

length 72      input: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
length 72 squeeze(.): <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>

length 72      input: <<<                                                    --- Harry S Truman  >>>
length 20 squeeze( ): <<< --- Harry S Truman >>>

length 72      input: <<<                                                    --- Harry S Truman  >>>
length 70 squeeze(-): <<<                                                    - Harry S Truman  >>>

length 72      input: <<<                                                    --- Harry S Truman  >>>
length 71 squeeze(r): <<<                                                    --- Hary S Truman  >>>

PHP

<lang PHP><?php

function squeezeString($string, $squeezeChar) {

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

}

function isSqueezable($string, $squeezeChar) {

   return ($string !== squeezeString($string, $squeezeChar));

}

$strings = array(

   ['-', '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln '],
   ['1', '..1111111111111111111111111111111111111111111111111111111111111117777888'],
   ['l', "I never give 'em hell, I just tell the truth, and they think it's hell. "],
   [' ', '                                                    --- Harry S Truman  '],
   ['9', '0112223333444445555556666666777777778888888889999999999'],
   ['e', "The better the 4-wheel drive, the further you'll be from help when ya get stuck!"],
   ['k', "The better the 4-wheel drive, the further you'll be from help when ya get stuck!"],

); foreach ($strings as $params) {

   list($char, $original) = $params;
   echo 'Original   : <<<', $original, '>>> (len=', mb_strlen($original), ')', PHP_EOL;
   if (isSqueezable($original, $char)) {
       $squeeze = squeezeString($original, $char);
       echo 'Squeeze(', $char, ') : <<<', $squeeze, '>>> (len=', mb_strlen($squeeze), ')', PHP_EOL, PHP_EOL;
   } else {
       echo 'Squeeze(', $char, ') : string is not squeezable (char=', $char, ')...', PHP_EOL, PHP_EOL;
   }

}</lang>

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

Original   : <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (len=72)
Squeeze(1) : <<<..17777888>>> (len=10)

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

Original   : <<<                                                    --- Harry S Truman  >>> (len=72)
Squeeze( ) : <<< --- Harry S Truman >>> (len=20)

Original   : <<<0112223333444445555556666666777777778888888889999999999>>> (len=55)
Squeeze(9) : <<<0112223333444445555556666666777777778888888889>>> (len=46)

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

Original   : <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>> (len=80)
Squeeze(k) : string is not squeezable (char=k)...

Prolog

<lang Prolog>squeeze_( [], _, [] ). squeeze_( [A], _, [A] ). squeeze_( [A,A|T], A, R ) :- squeeze_( [A|T], A, R ). squeeze_( [A,A|T], B, [A|R] ) :- dif( A, B ), squeeze_( [A|T], B, R ). squeeze_( [A,B|T], S, [A|R] ) :- dif( A, B ), squeeze_( [B|T], S, R ).

squeeze( Str, SqueezeChar, Collapsed ) :-

   string_chars( Str, Chars ),
   squeeze_( Chars, SqueezeChar, Result ),
   string_chars( Collapsed, Result ).</lang>
Output:
?- squeeze( "", ' ', New ).
New = "".

?- squeeze( "\"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 " .

?- squeeze( "..1111111111111111111111111111111111111111111111111111111111111117777888", '7', New ).
New = "..1111111111111111111111111111111111111111111111111111111111111117888" .

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

?- squeeze( "                                                    --- Harry S Truman  ", ' ', New ).
New = " --- Harry S Truman " .

?- squeeze( "                                                    --- Harry S Truman  ", '-', New ).
New = "                                                    - Harry S Truman  " .

?- squeeze( "                                                    --- Harry S Truman  ", 'r', New ).
New = "                                                    --- Hary S Truman  " .

To determine if a string is squeezed or not then the input and output strings should be the same, eg:

?- S = "..1111111111111111111111111111111111111111111111111111111111111117888", squeeze( S, '1', S ).
false.

?- S = "..1111111111111111111111111111111111111111111111111111111111111117888", squeeze( S, '7', S ).
S = "..1111111111111111111111111111111111111111111111111111111111111117888"

Python

<lang python>from itertools import groupby

def squeezer(s, txt):

   return .join(item if item == s else .join(grp)
                  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",
           "😍😀🙌💃😍😍😍🙌",
           ]
   squeezers = ' ,-,7,., -r,e,s,a,😍'.split(',')
   for txt, chars in zip(strings, squeezers):
       this = "Original"
       print(f"\n{this:14} Size: {len(txt)} «««{txt}»»»" )
       for ch in chars:
           this = f"Squeezer '{ch}'"
           sqz = squeezer(ch, txt)
           print(f"{this:>14} Size: {len(sqz)} «««{sqz}»»»" )</lang>
Output:
Original       Size: 0 «««»»»
  Squeezer ' ' Size: 0 «««»»»

Original       Size: 72 «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»»
  Squeezer '-' Size: 70 «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»»

Original       Size: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
  Squeezer '7' Size: 69 «««..1111111111111111111111111111111111111111111111111111111111111117888»»»

Original       Size: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
  Squeezer '.' Size: 72 «««I never give 'em hell, I just tell the truth, and they think it's hell. »»»

Original       Size: 72 «««                                                   ---  Harry S Truman  »»»
  Squeezer ' ' Size: 20 ««« --- Harry S Truman »»»
  Squeezer '-' Size: 70 «««                                                   -  Harry S Truman  »»»
  Squeezer 'r' Size: 71 «««                                                   ---  Hary S Truman  »»»

Original       Size: 80 «««The better the 4-wheel drive, the further you'll be from help when ya get stuck!»»»
  Squeezer 'e' Size: 79 «««The better the 4-whel drive, the further you'll be from help when ya get stuck!»»»

Original       Size: 16 «««headmistressship»»»
  Squeezer 's' Size: 14 «««headmistreship»»»

Original       Size: 8 «««aardvark»»»
  Squeezer 'a' Size: 7 «««ardvark»»»

Original       Size: 8 «««😍😀🙌💃😍😍😍🙌»»»
  Squeezer '😍' Size: 6 «««😍😀🙌💃😍🙌»»»

Raku

(formerly Perl 6)

Works with: Rakudo version 2019.07.1

<lang perl6>map {

   my $squeeze = $^phrase;
   sink $^reg;
   $squeeze ~~ s:g/($reg)$0+/$0/;
   printf "\nOriginal length: %d <<<%s>>>\nSqueezable on \"%s\": %s\nSqueezed length: %d <<<%s>>>\n",
     $phrase.chars, $phrase, $reg.uniname, $phrase ne $squeeze, $squeeze.chars, $squeeze

},

 , ' ', 
 '"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  ', ' ',
 '                                                    --- Harry S Truman  ', '-',
 '                                                    --- Harry S Truman  ', 'r'</lang>
Output:
Original length: 0 <<<>>>
Squeezable on "SPACE": False
Squeezed length: 0 <<<>>>

Original length: 72 <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>>
Squeezable on "HYPHEN-MINUS": True
Squeezed length: 70 <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>>

Original length: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
Squeezable on "DIGIT SEVEN": True
Squeezed length: 69 <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>

Original length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>
Squeezable on "FULL STOP": False
Squeezed length: 72 <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>>

Original length: 72 <<<                                                    --- Harry S Truman  >>>
Squeezable on "SPACE": True
Squeezed length: 20 <<< --- Harry S Truman >>>

Original length: 72 <<<                                                    --- Harry S Truman  >>>
Squeezable on "HYPHEN-MINUS": True
Squeezed length: 70 <<<                                                    - Harry S Truman  >>>

Original length: 72 <<<                                                    --- Harry S Truman  >>>
Squeezable on "LATIN SMALL LETTER R": True
Squeezed length: 71 <<<                                                    --- Hary S Truman  >>>

REXX

<lang rexx>/*REXX program "squeezes" all immediately repeated characters in a string (or strings). */ @.= /*define a default for the @. array. */

  1. .1= ' '; @.1=
  2. .2= '-'; @.2= '"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln '
  3. .3= '7'; @.3= ..1111111111111111111111111111111111111111111111111111111111111117777888
  4. .4= . ; @.4= "I never give 'em hell, I just tell the truth, and they think it's hell. "
  5. .5= ' '; @.5= ' --- Harry S Truman '
  6. .6= '-'; @.6= @.5
  7. .7= 'r'; @.7= @.5
    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. */
    say '    specified immediate repeatable character='     #.j      "   ('"c2x(#.j)"'x)"
    say '    length='right(L, 3)     "   input=«««" || @.j || '»»»'
    new= squeeze(@.j, #.j)
      w= length(new)
    say '    length='right(w, 3)     "  output=«««" || new || '»»»'
    end   /*j*/

exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ squeeze: procedure; parse arg y 1 $ 2,z /*get string; get immed. repeated char.*/

        if pos(z || z, y)==0  then return y     /*No repeated immediate char?  Return Y*/
                                                /* [↑]  Not really needed;  a speed─up.*/
                    do k=2  to length(y)        /*traipse through almost all the chars.*/
                    _= substr(y, k, 1)                      /*pick a character from  Y */
                    if _==right($, 1) & _==z then iterate   /*Same character?  Skip it.*/
                    $= $ || _                               /*append char., it's diff. */
                    end     /*j*/
        return $</lang>
output   when using the internal default inputs:
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable character=      ('20'x)
    length=  0    input=«««»»»
    length=  0   output=«««»»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable character= -    ('2D'x)
    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 »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable character= 7    ('37'x)
    length= 72    input=«««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
    length= 69   output=«««..1111111111111111111111111111111111111111111111111111111111111117888»»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable character= .    ('2E'x)
    length= 72    input=«««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
    length= 72   output=«««I never give 'em hell, I just tell the truth, and they think it's hell. »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable character=      ('20'x)
    length= 72    input=«««                                                   ---  Harry S Truman  »»»
    length= 20   output=««« --- Harry S Truman »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable character= -    ('2D'x)
    length= 72    input=«««                                                   ---  Harry S Truman  »»»
    length= 70   output=«««                                                   -  Harry S Truman  »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════
    specified immediate repeatable character= r    ('72'x)
    length= 72    input=«««                                                   ---  Harry S Truman  »»»
    length= 71   output=«««                                                   ---  Hary S Truman  »»»
═════════════════════════════════════════════════════════════════════════════════════════════════════════

Ruby

<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  ",
       "😍😀🙌💃😍😍😍🙌",]

squeeze_these = ["", "-", "7", ".", " -r", "😍"]

strings.zip(squeeze_these).each do |str, st|

 puts "original:     «««#{str}»»» (size #{str.size})"
 st.chars.each do |c|
   ssq = str.squeeze(c)
   puts "#{c.inspect}-squeezed: «««#{ssq}»»» (size #{ssq.size})" 
 end
 puts

end </lang>

Output:
original:     «««»»» (size 0)

original:     «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» (size 72)
"-"-squeezed: «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» (size 70)

original:     «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» (size 72)
"7"-squeezed: «««..1111111111111111111111111111111111111111111111111111111111111117888»»» (size 69)

original:     «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (size 72)
"."-squeezed: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (size 72)

original:     «««                                                   ---  Harry S Truman  »»» (size 72)
" "-squeezed: ««« --- Harry S Truman »»» (size 20)
"-"-squeezed: «««                                                   -  Harry S Truman  »»» (size 70)
"r"-squeezed: «««                                                   ---  Hary S Truman  »»» (size 71)

original:     «««😍😀🙌💃😍😍😍🙌»»» (size 8)
"😍"-squeezed: «««😍😀🙌💃😍🙌»»» (size 6)

Rust

See collapsible strings for the alternative implementation approach.

<lang Rust>fn squeezable_string<'a>(s: &'a str, squeezable: char) -> impl Iterator<Item = char> + 'a {

   let mut previous = None;
   s.chars().filter(move |c| match previous {
       Some(p) if p == squeezable && p == *c => false,
       _ => {
           previous = Some(*c);
           true
       }
   })

}

fn main() {

   fn show(input: &str, c: char) {
       println!("Squeeze: '{}'", c);
       println!("Input ({} chars): \t{}", input.chars().count(), input);
       let output: String = squeezable_string(input, c).collect();
       println!("Output ({} chars): \t{}", output.chars().count(), output);
       println!();
   }
   let harry = r#"I never give 'em hell, I just tell the truth, and they think it's hell.
   ---  Harry S Truman"#;
   #[rustfmt::skip]
   let inputs = [
       ("", ' '),
       (r#""If I were two-faced, would I be wearing this one?" --- Abraham Lincoln "#, '-'),
       ("..1111111111111111111111111111111111111111111111111111111111111117777888", '7'),
       (harry, ' '),
       (harry, '-'),
       (harry, 'r'),
       ("The better the 4-wheel drive, the further you'll be from help when ya get stuck!", 'e'),
       ("headmistressship", 's'),
   ];
   inputs.iter().for_each(|(input, c)| show(input, *c));

}</lang>

Sidef

<lang ruby>func squeeze(str, c) {

   str.gsub(Regex("(" + c.escape + ")" + '\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  ",
       "😍😀🙌💃😍😍😍🙌"]

var squeeze_these = ["", "-", "7", ".", " -r", "😍"]

[strings, squeeze_these].zip {|str,st|

   say "    original: «««#{str}»»» (length: #{str.len})"
   st.each {|c|
       var ssq = squeeze(str, c)
       say "'#{c}'-squeezed: «««#{ssq}»»» (length: #{ssq.len})"
   }
   say 

}</lang>

Output:
    original: «««»»» (length: 0)

    original: «««"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln »»» (length: 72)
'-'-squeezed: «««"If I were two-faced, would I be wearing this one?" - Abraham Lincoln »»» (length: 70)

    original: «««..1111111111111111111111111111111111111111111111111111111111111117777888»»» (length: 72)
'7'-squeezed: «««..1111111111111111111111111111111111111111111111111111111111111117888»»» (length: 69)

    original: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length: 72)
'.'-squeezed: «««I never give 'em hell, I just tell the truth, and they think it's hell. »»» (length: 72)

    original: «««                                                   ---  Harry S Truman  »»» (length: 72)
' '-squeezed: ««« --- Harry S Truman »»» (length: 20)
'-'-squeezed: «««                                                   -  Harry S Truman  »»» (length: 70)
'r'-squeezed: «««                                                   ---  Hary S Truman  »»» (length: 71)

    original: «««😍😀🙌💃😍😍😍🙌»»» (length: 8)
'😍'-squeezed: «««😍😀🙌💃😍🙌»»» (length: 6)

Tcl

<lang tcl># Set test data as a list pairing even and odd values

  1. as test string and squeeze character(s) respectively.

set test {

   {} {" "}
   {"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"}
   {The better the 4-wheel drive, the further you'll be from help when ya get stuck!} {"e"} ;# '
   {headmistressship} {"s"}

}

foreach {str chrs} $test {

   foreach c $chrs {
       # Escape non-word replacement characters (such as .)
       set c [regsub -all {\W} $c {\\&}]
       # Uses regexp lookbehind to detect repeated characters
       set re [subst -noback {($c)(\1+)}] ;# build expression
       set sub [regsub -all $re $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  69): ..1111111111111111111111111111111111111111111111111111111111111117888
----------------------
Original (length  72): I never give 'em hell, I just tell the truth, and they think it's hell.
Subbed   (length  72): I never give 'em hell, I just tell the truth, and they think it's hell.
----------------------
Original (length  72):                                                     --- Harry S Truman
Subbed   (length  20):  --- Harry S Truman
----------------------
Original (length  72):                                                     --- Harry S Truman
Subbed   (length  70):                                                     - Harry S Truman
----------------------
Original (length  72):                                                     --- Harry S Truman
Subbed   (length  71):                                                     --- 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  79): The better the 4-whel drive, the further you'll be from help when ya get stuck!
----------------------
Original (length  16): headmistressship
Subbed   (length  14): headmistreship
----------------------

Visual Basic .NET

Translation of: C#

<lang vbnet>Imports System.Linq.Enumerable

Module Module1

   Function Squeeze(s As String, c As Char) As String
       If String.IsNullOrEmpty(s) Then
           Return ""
       End If
       Return s(0) + New String(Range(1, s.Length - 1).Where(Function(i) s(i) <> c OrElse s(i) <> s(i - 1)).Select(Function(i) s(i)).ToArray())
   End Function
   Sub SqueezeAndPrint(s As String, c As Char)
       Console.WriteLine("squeeze: '{0}'", c)
       Console.WriteLine("old: {0} «««{1}»»»", s.Length, s)
       s = Squeeze(s, c)
       Console.WriteLine("new: {0} «««{1}»»»", s.Length, s)
   End Sub
   Sub Main()
       Const QUOTE = """"
       SqueezeAndPrint("", " ")
       SqueezeAndPrint(QUOTE & "If I were two-faced, would I be wearing this one?" & QUOTE & " --- Abraham Lincoln ", "-")
       SqueezeAndPrint("..1111111111111111111111111111111111111111111111111111111111111117777888", "7")
       SqueezeAndPrint("I never give 'em hell, I just tell the truth, and they think it's hell. ", ".")
       Dim s = "                                                    --- Harry S Truman  "
       SqueezeAndPrint(s, " ")
       SqueezeAndPrint(s, "-")
       SqueezeAndPrint(s, "r")
   End Sub

End Module</lang>

Output:
squeeze: ' '
old: 0 «««»»»
new: 0 «««»»»
squeeze: '-'
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 »»»
squeeze: '7'
old: 72 «««..1111111111111111111111111111111111111111111111111111111111111117777888»»»
new: 69 «««..1111111111111111111111111111111111111111111111111111111111111117888»»»
squeeze: '.'
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. »»»
squeeze: ' '
old: 72 «««                                                    --- Harry S Truman  »»»
new: 20 ««« --- Harry S Truman »»»
squeeze: '-'
old: 72 «««                                                    --- Harry S Truman  »»»
new: 70 «««                                                    - Harry S Truman  »»»
squeeze: 'r'
old: 72 «««                                                    --- Harry S Truman  »»»
new: 71 «««                                                    --- Hary S Truman  »»»

Wren

Translation of: Go
Library: Wren-fmt

<lang ecmascript>import "/fmt" for Fmt

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

   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] == ch.codePoints[0] && 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",
   "😍😀🙌💃😍😍😍🙌"

]

var chars = [ [" "], ["-"], ["7"], ["."], [" ", "-", "r"], ["e"], ["s"], ["a"], ["😍"] ]

var i = 0 for (s in strings) {

   for (ch in chars[i]) {
       var r = squeeze.call(s, ch)
       System.print("Specified character = '%(ch)'")
       System.print("original : length = %(Fmt.d(2, r[1])), string = «««%(s)»»»")
       System.print("squeezed : length = %(Fmt.d(2, r[2])), string = «««%(r[0])»»»\n")
   }
   i = i + 1

}</lang>

Output:
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 = «««😍😀🙌💃😍🙌»»»

zkl

<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

}</lang> <lang zkl>strings:=T( T("",""), T("-","\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "), T("7","..1111111111111111111111111111111111111111111111111111111111111117777888"), T(" ","I never give 'em hell, I just tell the truth, and they think it's hell. "), T(" "," --- Harry S Truman "), T("-"," --- Harry S Truman "), T("r"," --- Harry S Truman "), T("e","The better the 4-wheel drive, the further you'll be from help when ya get stuck!"), T("s","headmistressship"), T("\Ubd;","\Ubc;\Ubd;\Ubd;\Ube;"), );

foreach c,s in (strings){

  println("Squeeze: \"",c,"\"");
  println("Before: %2d <<<%s>>>".fmt(s.len(-8),s));
  sstr:=squeeze(c,s);
  println("After:  %2d <<<%s>>>\n".fmt(sstr.len(-8),sstr));

}</lang>

Output:
Squeeze: ""
Before:  0 <<<>>>
After:   0 <<<>>>

Squeeze: "-"
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 >>>

Squeeze: "7"
Before: 72 <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>>
After:  69 <<<..1111111111111111111111111111111111111111111111111111111111111117888>>>

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

Squeeze: " "
Before: 72 <<<                                                    --- Harry S Truman  >>>
After:  20 <<< --- Harry S Truman >>>

Squeeze: "-"
Before: 72 <<<                                                    --- Harry S Truman  >>>
After:  70 <<<                                                    - Harry S Truman  >>>

Squeeze: "r"
Before: 72 <<<                                                   ---  Harry S Truman  >>>
After:  71 <<<                                                   ---  Hary S Truman  >>>

Squeeze: "e"
Before: 80 <<<The better the 4-wheel drive, the further you'll be from help when ya get stuck!>>>
After:  79 <<<The better the 4-whel drive, the further you'll be from help when ya get stuck!>>>

Squeeze: "s"
Before: 16 <<<headmistressship>>>
After:  14 <<<headmistreship>>>

Squeeze: "½"
Before:  4 <<<¼½½¾>>>
After:   3 <<<¼½¾>>>