Jay Gorio
Jay Gorio

Reputation: 335

Count number of letters in CMD and batchfile

I have this project using batch file.My code below can count characters when user type inputs (numbers, letters, symbols),but i want the program count only letters. Can you help me out with this problem. My code is shown below.

@ECHO OFF
echo.

:a
REM Set "string" variable
SET /p string="Type characters to Count:"
REM Set the value of temporary variable to the value of "string" variable
SET temp_str=%string%
REM Initialize counter
SET str_len=1

:loop
if defined temp_str (
REM Remove the first character from the temporary string variable and increment 
REM counter by 1. Countinue to loop until the value of temp_str is empty string.
SET temp_str=%temp_str:~1%
SET /A str_len += 1
GOTO loop
)

REM Echo the actual string value and its length.
ECHO %string% is %str_len% characters long!
set /p prompt="Do you want to continue?Y/N:"
if %prompt%==Y goto a
goto b

:b
pause
exit

Upvotes: 1

Views: 2470

Answers (3)

MC ND
MC ND

Reputation: 70923

@ECHO OFF
    setlocal enableextensions disabledelayedexpansion

:getString
    echo(
    REM Set "string" variable
    set "string="
    SET /p "string=Type characters to Count:"

:calculateLength
    if not defined string ( set "str_len=0" ) else (
        for /f %%a in ('
            cmd /u /v /q /c"(echo(!string!)" %= output unicode string =%
            ^| find /v ""                    %= separate lines =%
            ^| findstr /r /c:"[a-zA-Z]"      %= filter characters =%
            ^| find /c /v ""                 %= count remaining lines =%
        ') do set "str_len=%%a"
    )

:show    
    REM Echo the actual string value and its length.
    ECHO [%string%] is %str_len% letters long!
    echo(
    set /p "prompt=Do you want to continue? Y/n: " || set "prompt=Y"
    if /i "%prompt%"=="Y" goto :getString

    pause
    exit /b

This uses a simple trick. If we start a unicode cmd instance, its output (the result from echo in this case) is unicode, that is, two bytes per character, usually a null and the real character.

This output is processed by a filter, more or find, that will process the nulls as line terminators, splitting the output string into lines, one line for each input character.

This set of lines are filtered with findstr to only allow letters. The remaining lines are counted with a find /c /v "" command that will count non empty lines.

edited to adapt to comments.

Input a block of lines and process them

@ECHO OFF
    setlocal enableextensions disabledelayedexpansion
    set "tempFile=%temp%\%random%%random%%random%.tmp"

:getString
    echo(
    echo(Please, type your text and end input pressing F6 and Enter
    copy con "%tempfile%" >nul 2>nul

:calculateLength
    for /f %%a in ('
        cmd /u /q /c"(type "%tempFile%")" %= output unicode string =%
        ^| find /v ""                     %= separate lines =%
        ^| findstr /r /c:"[a-zA-Z]"       %= filter characters =%
        ^| find /c /v ""                  %= count remaining lines =%
    ') do set "str_len=%%a"

:show    
    ECHO input data is %str_len% letters long!
    echo(
    del /q "%tempFile%" 2>nul 
    set /p "prompt=Do you want to continue? Y/n: " || set "prompt=Y"
    if /i "%prompt%"=="Y" goto :getString

    pause
    exit /b

Upvotes: 3

Aacini
Aacini

Reputation: 67216

@ECHO OFF
setlocal EnableDelayedExpansion

REM Define a string with all letters:
SET letters=ABCDEFGHIJKLMNOPQRSTUVWXYZ

echo.

:a
REM Set "string" variable
SET /p string="Type characters to Count:"
REM Set the value of temporary variable to the value of "string" variable
SET temp_str=%string%
REM Initialize counter
SET str_letters=0

:loop
if defined temp_str (
REM Get the first character from temporary string variable
SET char=%temp_str:~0,1%
REM If the character is letter, count it
if "!letters:%char%=!" neq "%letters%" SET /A str_letters += 1
REM Remove the first character from the temporary string variable.
REM Countinue to loop until the value of temp_str is empty string.
SET temp_str=%temp_str:~1%
GOTO loop
)

REM Echo the actual string value and its number of letters.
ECHO %string% have %str_letters% letters
set /p prompt="Do you want to continue?Y/N:"
if %prompt%==Y goto a
goto b

:b
pause
exit

Upvotes: 0

You are setting initial string length to 1 in line

SET str_len=1

change it to 0 like so:

SET str_len=0

Upvotes: 0

Related Questions