batch to search for duplicate letters in string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
findstr
Posts: 17
Joined: 09 Jun 2021 12:36

batch to search for duplicate letters in string

#1 Post by findstr » 25 Jun 2021 04:45

Hello!
If I am getting strings with 100 random symbols in them, how can I find duplicates in them?
From string "ABCDDEFGH##12345..." the script should return "DD" and "##",
from string "1223..." the script should return "22", and so on.
All strings are 100 characters in length:
"R#6-O0W9OXRi&FQSf1&ZcBJlwhE886D%2CZ+-L7B+Z2khH1UG3NbDC&&Xj@TvS!DVEA1RNuWmup^HJnp$O)d!Yh(8^cVWTbF5GxN"
In this string "88" and "&&" are duplicates.
Thanks!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch to search for duplicate letters in string

#2 Post by aGerman » 25 Jun 2021 05:29

Note that I had to double the percent sign in the assignment to finally have one in the string.

Code: Select all

@echo off

setlocal DisableDelayedExpansion
set "str=R#6-O0W9OXRi&FQSf1&ZcBJlwhE886D%%2CZ+-L7B+Z2khH1UG3NbDC&&Xj@TvS!DVEA1RNuWmup^HJnp$O)d!Yh(8^cVWTbF5GxN"

setlocal EnableDelayedExpansion
set "prev=!str:~0,1!"
for /l %%i in (1 1 99) do (
  set "curr=!str:~%%i,1!"
  if "!prev!"=="!curr!" echo !prev!!curr!
  set "prev=!curr!"
)

pause
Steffen

Aacini
Expert
Posts: 1910
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: batch to search for duplicate letters in string

#3 Post by Aacini » 25 Jun 2021 08:42

Code: Select all

@echo off

setlocal DisableDelayedExpansion
set "str=R#6-O0W9OXRi&FQSf1&ZcBJlwhE886D%%2CZ+-L7B+Z2khH1UG3NbDC&&Xj@TvS!DVEA1RNuWmup^HJnp$O)d!Yh(8^cVWTbF5GxN"

setlocal EnableDelayedExpansion
for /L %%i in (0,1,98) do (
   if "!str:~%%i,2!" equ "!str:~%%i,1!!str:~%%i,1!" echo !str:~%%i,2!
)
Antonio

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: batch to search for duplicate letters in string

#4 Post by Jer » 01 Jul 2021 12:49

I did not see %% after running the batch codes. An extra step may be needed to catch double percents.
I added other special characters doubled. Quotes are not addressed; an odd number caused an error.

Jerry

Code: Select all

@echo off

set "str=R#**6-O0W9OXRi&FQSf1&ZcBJlwhE86D%%2CZ+-L7B+^Z2khH1<<UG>>3NbDC&&Xj@TvS!DVEA1RNuWmup^^HJnp$O)d!Yh(8^cVWTbF5GxN!!"
call :strLen str len
set /A "len-=1"

setlocal EnableDelayedExpansion
if "!str:%%=!" neq "!str!" echo %%%%
for /L %%i in (0,1,%len%) do (
  if "!str:~%%i,2!" equ "!str:~%%i,1!!str:~%%i,1!" echo !str:~%%i,2!
)
endlocal & set "str=" & set "len="
goto :eof

:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:$source https://www.dostips.com
 (   SETLOCAL ENABLEDELAYEDEXPANSION
     set "s__=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                      rem it also avoids trouble in case of empty string
     set "len=0"
     for /L %%A in (12 -1 0) dO (
         set /a "len|=1<<%%A"
         for %%B in (!len!)Do if "!s__:~%%B,1!"=="" set /a "len&=~1<<%%A"
     )
 )
 ( ENDLOCAL & REM RETURN VALUES
     IF "%~2" NEQ "" SET /a "%~2=%len%"
 )
 EXIT /b
>testdbls
%%
**
<<
>>
&&
^^
!!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch to search for duplicate letters in string

#5 Post by aGerman » 01 Jul 2021 14:12

Jer wrote:
01 Jul 2021 12:49
I did not see %% after running the batch codes. An extra step may be needed to catch double percents.
In the original post is only one percent sign in the string. I doubled it in the SET statement to have one percent sign in the resulting variable. Otherwise it would have been stripped entirely. So, it's technically correct if it doesn't find any double percent signs.

Steffen

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: batch to search for duplicate letters in string

#6 Post by Jer » 01 Jul 2021 14:56

aGerman, how does your code, typed out below, display %% for you and not for me? :o
c:\Temp\Dosbatch>testAG
88
&&
Press any key to continue . . .

c:\Temp\Dosbatch>type testag.bat
@echo off

setlocal DisableDelayedExpansion
set "str=R#6-O0W9OXRi&FQSf1&ZcBJlwhE886D%%2CZ+-L7B+Z2khH1UG3NbDC&&Xj@TvS!DVEA1RNuWmup^HJnp$O)d!Yh(8^cVWTbF5GxN"

setlocal EnableDelayedExpansion
set "prev=!str:~0,1!"
for /l %%i in (1 1 99) do (
set "curr=!str:~%%i,1!"
if "!prev!"=="!curr!" echo !prev!!curr!
set "prev=!curr!"
)

pause

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch to search for duplicate letters in string

#7 Post by aGerman » 01 Jul 2021 15:19

how does your code, typed out below, display %% for you and not for me?
It did not. And that's the point :!: Double percents in the assignment of str yield a single percent in the variable value. Why should it ever report double percent signs? :lol:

Steffen

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: batch to search for duplicate letters in string

#8 Post by Jer » 01 Jul 2021 22:59

Understood. I thought that a line of code to display %% if found doubled in the string might be helpful.
My thinking was that literal doubled percents, like all other characters, but maybe not quotes, are a target for identification.
:)
Jerry

Post Reply