local fmt = require "fmt" require "uchar" -- Returns squeezed string, original and new lengths in -- unicode code points (not normalized). local function squeeze(s, ch) local c = uchar.of(s) local c2 = uchar.of(ch) local le = c:len() if le < 2 then return s, le, le end for i = le - 1, 1, -1 do if c:get(i) == c2:get(1) and c:get(i) == c:get(i + 1) then c:remove(i) end end return c:tostring(), le, c:len() end local 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", "๐Ÿ˜๐Ÿ˜€๐Ÿ™Œ๐Ÿ’ƒ๐Ÿ˜๐Ÿ˜๐Ÿ˜๐Ÿ™Œ" } local chars = { {" "}, {"-"}, {"7"}, {"."}, {" ", "-", "r"}, {"e"}, {"s"}, {"a"}, {"๐Ÿ˜"} } for i, s in strings do for chars[i] as ch do local cc, le, lcc = squeeze(s, ch) print($"Specified character = '{ch}'") fmt.print("original : length = %2d, string = ยซยซยซ%sยปยปยป", le, s) fmt.print("squeezed : length = %2d, string = ยซยซยซ%sยปยปยป\n", lcc, cc) end end