count characters of a input string in batch file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
kid
Posts: 2
Joined: 03 Jun 2023 11:37

count characters of a input string in batch file

#1 Post by kid » 08 Jun 2023 10:10

Hi, I want to write a batch script that can count uppercase and lowercase characaters and digits and special characaters of input string for example if I enter this:
7f(92_/%67G46sMg_<2kkDgit4_+^H!aYCe=_-wQW4S=taMnGBDQLGx(F>^>(SFob@eUxFH0-2#yAJK/i!Q=BiWJ@z/4KKwNF#T=
it will show me this:

upper : 34
lower : 24
digit : 13
special_chars : 29

how should i write that?
please help me

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: count characters of a input string in batch file

#2 Post by T3RRY » 15 Jun 2023 21:01

kid wrote:
08 Jun 2023 10:10
Hi, I want to write a batch script that can count uppercase and lowercase characaters and digits and special characaters of input string for example if I enter this:
7f(92_/%67G46sMg_<2kkDgit4_+^H!aYCe=_-wQW4S=taMnGBDQLGx(F>^>(SFob@eUxFH0-2#yAJK/i!Q=BiWJ@z/4KKwNF#T=
it will show me this:

upper : 34
lower : 24
digit : 13
special_chars : 29

how should i write that?
please help me
First, you would start with determining the length of the string, then by assessing each character along the length of the string to determine type.

Code: Select all

@Echo off
(Set \n=^^^

%= \n macro newline variable - Do not Modify =%)

 Setlocal EnableDelayedExpansion


rem StrLen Macro source: https://www.dostips.com/forum/memberlist.php?mode=viewprofile&u=417
rem Environment independent macro definition source: https://www.dostips.com/forum/viewtopic.php?t=9265

For /f %%! in ("! ! ^^^!") Do ^
%= FIRST CHARACTER FOLLOWING THIS REMARK MUST NOT BE EMPTY =%Set ^"@StrLen=for %%n in (1 2) do if "%%n"=="2" (%\n%
		For /F "tokens=1 delims=, " %%G in ("%%!argv%%!")Do (%\n%
			Set "[StrLen]tmpStr=%%!%%~G%%!"%\n%
			Set "[StrLen]Len=0"%\n%
			Set "[StrLen]RV=%%G.Len"%\n%
			If not "%%![StrLen]tmpStr%%!" == "" For %%N IN (4096 2048 1024 512 256 128 64 32 16 8 4 2 1)Do (%\n%
				If not "%%![StrLen]tmpStr:~%%N,1%%!" == "" (%\n%
					Set /A "[StrLen]Len += %%N"%\n%
					Set "[StrLen]tmpStr=%%![StrLen]tmpStr:~%%N%%!"%\n%
		)	)	)%\n%
  		If not "%%![StrLen]tmpStr%%!" == "" (%\n%
			For %%v in ("%%![StrLen]RV%%!")Do For %%u in ("%%![StrLen]Len%%!")Do Endlocal ^& (%\n%
				Set /A "%%~v=%%~u + 1,%%~v[0i]= %%~u"%\n%
			)%\n%
		)Else (%\n%
			For %%v in ("%%![StrLen]RV%%!")Do Endlocal ^& (%\n%
				Set /A "%%~v=0,%%~v[0i]=0"%\n%
		)	)%\n%
 	)Else Setlocal EnableDelayedExpansion ^& set argv=,"%= Escape the following LF to define another Macro within this loop =%

Set "testString=7f(92_/%%67G46sMg_<2kkDgit4_+^^H^!aYCe=_-wQW4S=taMnGBDQLGx(F>^^>(SFob@eUxFH0-2#yAJK/i^!Q=BiWJ@z/4KKwNF#T="

Call:GetStringData testString
Set testStr

Pause
Endlocal & Goto:Eof

:GetStringData
	%@StrLen% %~1
	Set /a "%~1.type.Upper=0,%~1.type.Lower=0,%~1.type.Num=0,%~1.type.Non_AlphaNum=0"
	>Nul (
		For /l %%i in (0 1 !%~1.Len[0i]!)Do (
			Set "typeAssigned="
			Set "char=!%~1:~%%i,1!"
			If "!Char!" == "^!" Set /A "%~1.type.NON_AlphaNum+=1","typeAssigned=1"
			If not defined typeAssigned For /f "delims=0123456789" %%c in ("!Char!.")Do if "%%c"=="." (
				Set /A "%~1.type.Num+=1","typeAssigned=1"
			)
			If not defined typeAssigned For /f "delims=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" %%c in ("!Char!.")Do (
				if "%%c"=="." (
					Echo(!char!|Findstr /R "[ABCDEFGGHIJKLMNOPQRSTUVWXYZ]" && Set /A "%~1.type.Upper+=1","typeAssigned=1" || Set /A "%~1.type.Lower+=1","typeAssigned=1"
				)Else Set /A "%~1.type.NON_AlphaNum+=1"
	)	)	)
Exit /b 0

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

Re: count characters of a input string in batch file

#3 Post by Aacini » 16 Jun 2023 00:04

There are a lot of different ways to solve this problem. This is another (simpler, I think) method:

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Input the string
set /P "string=" < test.txt
echo The string:
echo !string!
echo/

set "digits=0123456789"
set "letters=ABCDEFGHIJKLMNOPQRSTUVWXYZ"

rem Separate string in chars
for /F "delims=" %%a in ('cmd /Q /U /C set /P "=!string!" ^< NUL ^| find /V ""') do (

   rem Two special cases first: exclamation mark and equal-sign
   if "%%a" equ "" (
      set /A special+=1
   ) else if "%%a" equ "=" (
      set /A special+=1

   ) else if "!letters:%%a=!" equ "%letters%" (
      if "!digits:%%a=!" equ "%digits%" (
         set /A "special+=1"
      ) else (
         set /A "digit+=1"
      )
   ) else (
      if "!letters:%%a=%%a!" equ "%letters%" (
         set /A "upper+=1"
      ) else (
         set /A "lower+=1"
      )
   )

)

echo upper:   %upper%
echo lower:   %lower%
echo digit:   %digit%
echo special: %special%
Output:

Code: Select all

The string:
7f(92_/%67G46sMg_<2kkDgit4_+^H!aYCe=_-wQW4S=taMnGBDQLGx(F>^>(SFob@eUxFH0-2#yAJK/i!Q=BiWJ@z/4KKwNF#T=

upper:   34
lower:   24
digit:   13
special: 29
Antonio

T3RRY
Posts: 250
Joined: 06 May 2020 10:14

Re: count characters of a input string in batch file

#4 Post by T3RRY » 16 Jun 2023 01:57

Aacini wrote:
16 Jun 2023 00:04
There are a lot of different ways to solve this problem. This is another (simpler, I think) method:

Antonio
Provided of course that the input string doesn't start with leading `=` or whitespace.

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

Re: count characters of a input string in batch file

#5 Post by Jer » 18 Jun 2023 18:32

Are there not 3 special cases? Adding an asterisk increases the lower count.
Adding a special case for asterisk counts it as a special character.

Code: Select all

 
 ...
   ) else if "%%a" equ "*" (
      set /A special+=1
  ...
       
Jerry

Post Reply