Testing specific characters in string input

April 27, 2010 at 17:08:05
Specs: Windows 7
I would like to test a specific character in a inputted string.
So the code would start like this:
set /p string="Enter here:"

And from there (after user inputs their word), I need it to do this:

Number of characters in "string" =
If "character 1" of "string" = "X" then do this
If "character 2" of "string" = "X" then do this

Is this possible? If I am given some sample code, that would be enough.

See More: Testing specific characters in string input

Report •

April 27, 2010 at 21:32:48
Ok, I've got this so far for testing:

"@echo off
set nc=1
set n1=0
set n2=0
set n3=0
set /p string="Enter word(s) here:"
set sc=%string%

if "%sc:~1%" neq "" (
set /a nc+=1
set sc=%sc:~1%
goto :loop

if %string:~0,1% equ a (
set n1=a
if %string:~0,1% equ b (
set n1=b
if %string:~0,1% equ c (
set n1=c

if %nc% gtr 1 (
if %string:~1,1% equ a (
set n2=a
if %string:~1,1% equ b (
set n2=b
if %string:~1,1% equ c (
set n2=c

if %nc% gtr 2 (
if %string:~2,1% equ a (
set n3=a
if %string:~2,1% equ b (
set n3=b
if %string:~2,1% equ c (
set n3=c


So that works fine if I input 3 letters (a, b or c). If I put any less than 3... I get an error "a unexpected at this time"

How can I fix this?

Report •

April 27, 2010 at 22:00:27
I can't be more specific because you weren't.
you say "X" - do you mean literally "X", or just "some char"
so X can be a constant "X" or a specific letter, and then there's location to deal with. "X" in 1, "X" in 2...
or "A" in 1, "Z" in 2... or as i coded, possibility of both
posn 2 = A, posn3=X ... etc etc etc ad infinitum!!!!!
i left all the debug rigging in place, knowing you'll need it.
this can isolate the chars and count them (up to max):
@echo off & setlocal enabledelayedexpansion
set max=100
set /p str=:
for /L %%a in (0 1 %max%) do (
echo %%a
set chr=!str:~%%a,1!
set /a count=%%a
if "!chr!" equ "" goto :done
call :%%a %%a !chr! 2>nul
call :!chr! %%a !chr! 2>nul
call :!chr!%%a %%a !chr! 2>nul
goto :eof
echo based on ordinal, %0 letter %1 is %2
goto :eof
echo based on letter-content, %0 letter %1 is %2
goto :eof
echo based on ordinal AND lettercontent, %0 letter %1 is %2
goto :eof
echo chars: %count%
:::---- edit oops!
need to quit taking those beer breaks!
crossed in the mail!

Report •
Related Solutions

Ask Question