Solved Comparing characters in 2 Strings

September 30, 2012 at 12:45:43
Specs: Windows 7
Alright so I want to compare the characters in two strings. One string is a user input (the password guess) and the other string is the correct password (variable name is %pass%). I want to compare how many characters they guess right in the password. If the password is 7 characters, and their guess is 7 characters, it would look like
3/7 Correct
However, if the input is more or less words than the password it could look something like:
Password is Cayote
Guess is Californina
2/6 correct and it would just ignore the rest of the characters.

So here is the psuedocode-ish idea
Check the amount of characters in %pass% and set that number to be variable %charnum%
For %charnum% characters in the user input compare them with the characters in %pass%
If character 1 in pass equals character 1 in guess do set %correctchars%=%correctchars%+1
If character 2 in ... etc

The password is always 5, 6, 7, or 8 characters btw. The user input can be any number of characters.

Could anyone please give me some code examples on how to accomplish this kind of task.

See More: Comparing characters in 2 Strings

Report •

September 30, 2012 at 18:23:17
✔ Best Answer
::===== begin script
@echo off & setlocal enabledelayedexpansion
set pass=whatispass
set guess=whatisguess
call :len %pass%
set lenp=%len%
call :len %guess%
set leng=%len%
set corr=0
if leng gtr lenp (set /a loop=lenp-1) else (set /a loop=leng-1)
:: debug 3lines
echo loop:%loop%
echo length pass: %lenp%
echo length guess: %leng%
for /L %%a in (0 1 %loop%) do (
set a=!pass:~%%a,1!
set b=!guess:~%%a,1!
::debug 2lines
echo a:!a!
echo b:!b!
if !a! neq !b! goto :display
set /a corr+=1
echo %corr% out of %lenp%
goto :eof

set work=%1
set len=0
for /L %%a in (0 1 100) do (
set v=!work:~%%a,1!
if not defined v goto :eof
set /a len+=1
::==== end script

Report •

October 1, 2012 at 04:43:19
Alright thanks, it works perfectly. I was wondering if you could explain a few things though. What is !a! and !b!, like why do they have the exclamation marks? Also, whats ~%%a,1!? Could you maybe comment a few lines in there to show whats going on so I can implement these techniques into my code if needed in other places.

Report •

October 1, 2012 at 13:12:03
Here is a great site for learning some batch techniques with code examples.


Report •

Related Solutions

October 1, 2012 at 17:56:16
The exclams are equivalent to percents, IF enabledelayedexpansion is active. if not, they no longer work. !a! is same as %a%, but it is now accessible inside a code-block. A code-block is any code inside (). I recommend a thorough study of these listings:
for /?
set /?
if /?

They explain things better than i can. the other thing, for this code:

for /L %%a in (0 1 100) do (
set v=!work:~%%a,1!
if not defined v goto :eof

!work! is the incoming argument %1 (the string whose length we want to get).
the operation after "set v=" is extracting one byte from that string: ~%%a,1
and the byte extracted is advancing by one for every loop defined by the "for /L" parameters. %%a goes from 0 to (an arbitrary) 100 in increments of one. When the byte is null (v is not defined), then the loop exits with the length stored in var. %len% (or !len!)
The downside of "enabledelayedexpansion", in your application, is that it makes the use of "!" for any other purpose tricky and cumbersome. So your random-char generator has to deal with that issue, one way or another.
hope this helps. If you have specific questions later, i will try to contribute to the assistance dialogue of the forum. good luck with your project! :-)

Report •

October 4, 2012 at 09:03:04
thanks a ton! Its starting to make a lot more sense.

Report •

Ask Question