kemal ozdogan
kemal ozdogan

Reputation: 81

how to count each letter in a sentence withoud the blank spaces in batch

I am trying to count all the letters that are in a sentence and I am trying to echo it on screen, but I couldn't get it to work I have tried to set a count so it goes up by 1 for each letter but couldn't get it to work. This is my code:

@echo off
:loop
SET Input=
set /p Input=jouwscript
IF "%Input%"=="" (
echo Write atleast 1 word
goto loop
) else (
    FOR %%a IN (%Input%) DO (
            ECHO %%a
        )
)

Upvotes: 0

Views: 327

Answers (4)

Compo
Compo

Reputation: 38604

As you have not stipulated exactly what you're looking to do, or exactly how you'd like your output, here's something a little bit different to the other current answers:

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
Set "input=" & Set /P "input=jouwscript>" & If Not Defined input GoTo :EOF
For /F Delims^=^ EOL^= %%G In ('(%%SystemRoot%%\System32\cmd.exe /V /U /S /D /C 
 "Set /P "^=!input: ^=!" 0<NUL"^) ^| %%SystemRoot%%\System32\find.exe /N /V ""'
) Do Set /P "=%%G" 0<NUL & Echo(
Pause

The last line is optional, I've included it only for testing by GUI, (doubleclick).


As an extension of the above idea, you could of course capture each character as a variable too:

@Echo Off & SetLocal EnableExtensions DisableDelayedExpansion
(For /F "Delims==" %%G In ('"(Set Char[) 2>NUL"') Do Set "%%G=") & Set "i=0" 
Set "input=" & Set /P "input=jouwscript>" & If Not Defined input GoTo :EOF
For /F Delims^=^ EOL^= %%G In ('(%%SystemRoot%%\System32\cmd.exe /V /U /S /D /C 
 "Set /P "^=!input: ^=!" 0<NUL"^) ^| %%SystemRoot%%\System32\find.exe /V ""'
) Do (Set "Char[0]= %%G" & For /F "Tokens=2 Delims= " %%H In ('Set Char[0]'
    ) Do Set /A i+=1 & SetLocal EnableDelayedExpansion & For %%I In (!i!
    ) Do EndLocal & Set "Char[%%I]=%%H" & Set "Char[0]=")
Echo There were %i% non space characters.
If Defined Char[1] For /L %%G In (1,1,%i%) Do Set Char[%%G]
Pause

Once again, the last line is optional, I've included it only for testing by GUI, (doubleclick).

Upvotes: 1

Stephan
Stephan

Reputation: 56180

If you are willing to accept a temporary file, it gets very simple:
Just write the string without spaces to a file and get the file size:

@Echo off
setlocal
set /p "input=Enter sentence: "
<nul >temp set /p "=%input: =%"
for %%a in (temp) do echo "%input%" has %%~za letters.

This counts all characters but spaces (also TABs, numbers, punctuation marks, and even poison chars (like <>&|%"!))

Upvotes: 2

T3RR0R
T3RR0R

Reputation: 2951

Here is a method that counts characters and words input. Word length maximum of 30 characters assumed. The basis is substring modification with regards to counting letters - iterating over a given number of character spaces and incrementing the letter count for each non empty letter space.

Substring modification is also used to handle the presence of ! within strings. If you need to count letters only, ignoring characters, you will need to adjust the below to remove non letter characters from the string prior to commencing the nested loop that performs the counts. Alternately, Use findstr to reject any input that contains non letter/ whitespace characters.

@Echo off
 Call :Getinput Sentence
 Echo(Your Sentence: "%Sentence%"
 Echo(Contains: %WC% Words and %LC% Letters
Goto :Eof

:GetInput
:# Disable DE Environment to allow substitution of ! Expansion character
 Setlocal DISABLEDELAYEDEXPANSION

:# Nul Letter and Word Count Variables
 Set /A "LC=WC=0"

:# Nul existing variable value
 Set "input="

:# Additional safegaurd; Doublequote input command. 
 Set /P "input=Enter Sentence: "

:# Flag input as not entered if true; exit function
 If Not Defined Input Exit /B 1

:# Substitute exclamations for counting during DelayedExpansion Environment
 Set "input=%input:!=[EXC]%"

:# Allow safe use of input strings, And expansion of current value of variables when modified in code blocks:
 Setlocal EnableDelayedExpansion

:# Assign Word Value; Modify ! Expansion Char for counting; Count each Character in Word
 For %%W in (!Input!)Do (
  Set "Word=%%W"
  Set "Word=!Word:[EXC]=#!"
  Set /A WC+=1
  For /L %%L in (0 1 30)Do If Not "!Word:~%%L,1!" == "" (
   Set /A LC+=1
  )
 )

:# Exit DelayedExpansion Environments
 For %%i in (1 2)Do Endlocal & (
  Set "LC=%LC%"
  Set "WC=%WC%"
  Set "Input=%Input%"
 )

:# Substitute exclamation marks back into the input string after exiting DE environment
:# Assign value to Arg 1
 Set "%~1=%Input:[EXC]=!%"

:# Flag input as entered
 Exit /B 0

output:

C:\Users\tcdou>tout
Enter Sentence: test! one
Your Sentence: "test! one"
Contains: 2 Words and 8 Letters

C:\Users\tcdou>tout
Enter Sentence: test! ! two
Your Sentence: "test! ! two"
Contains: 3 Words and 9 Letters

C:\Users\tcdou>

Upvotes: 1

lit
lit

Reputation: 16236

If you are on a supported Windows system and are able to use the programs already on it, you could easily do this using a single PowerShell command. This command will replace whitespace (space and tab), then calculate the length of the resulting string.

SET "Input=now is the time"
FOR /F %%A IN ('powershell -NoLogo -NoProfile -Command ^
    "('%Input%' -replace '\s+','').Length"') DO (SET /A "COUNT=%%A")
ECHO COUNT is %COUNT%

Upvotes: 1

Related Questions