如何在没有空格的情况下分批计算句子中的每个字母



我正在尝试计数一个句子中的所有字母,并试图在屏幕上回显它,但我无法使其工作。我已经尝试设置了一个计数,因此每个字母的计数都会增加1,但无法使其起作用。这是我的代码:

@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
)
)

如果您愿意接受一个临时文件,它会非常简单:
只需将不带空格的字符串写入文件,即可获得文件大小:

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

这会计算除空格之外的所有字符(还有TAB、数字、标点符号,甚至毒字符(如<>&|%"!((

如果您在受支持的Windows系统上,并且能够使用该系统上已有的程序,则可以使用单个PowerShell命令轻松完成此操作。此命令将替换空白(空格和制表符(,然后计算结果字符串的长度。

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%

这里有一个计算输入的字符和单词的方法。假设单词长度最多为30个字符。其基础是关于字母计数的子串修改——在给定数量的字符空间上迭代,并增加每个非空字母空间的字母计数。

子字符串修改也用于处理字符串中!的存在。如果只需要计算字母,忽略字符,则在开始执行计数的嵌套循环之前,需要调整以下内容以从字符串中删除非字母字符。或者,使用findstr拒绝任何包含非字母/空白字符的输入。

@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

输出:

C:Userstcdou>tout
Enter Sentence: test! one
Your Sentence: "test! one"
Contains: 2 Words and 8 Letters
C:Userstcdou>tout
Enter Sentence: test! ! two
Your Sentence: "test! ! two"
Contains: 3 Words and 9 Letters
C:Userstcdou>

由于您没有确切地规定您想要做什么,或者您希望输出的方式,因此这里有一些与其他当前答案有点不同的东西:

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

最后一行是可选的,我只包含它用于GUI测试(双击(


作为上述想法的扩展,您当然也可以将每个字符捕获为一个变量:

@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%%System32cmd.exe /V /U /S /D /C 
"Set /P "^=!input: ^=!" 0<NUL"^) ^| %%SystemRoot%%System32find.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

再一次,最后一行是可选的,我只为通过GUI进行测试而包含了它(双击(

最新更新