Skip to main content

How to Check if a String is a Palindrome in Batch Script

A string is considered a palindrome if it reads exactly the same backward as it does forward. For example, "RACECAR" or "MADAM". By the end of this guide, you will master iterative substring concatenation, variable length loops, and robust case-insensitive condition checking within a pure Batch configuration.

Let's build a string palindrome checker directly inside a Windows Batch Script without relying on Python or PowerShell. Reversing strings character by character and logically comparing the inverted form to its original provides an excellent lesson on parsing dynamic memory arrays natively in the cmd prompt.

The Theory Behind String Reversal

The Command Prompt does not have a built-in "reverse" string function. Instead, any attempt to dynamically flip a string must be manually orchestrated using explicit length counters and character indexing (!var:~x,1!).

To reverse "DOG" into "GOD", the engine must evaluate index 2 ("G"), then index 1 ("O"), then index 0 ("D"), prepending each character to an iteratively growing buffer variable.

Building the Reversal Loop

To construct a reversal algorithm, we leverage delayed expansion and a GOTO loop to extract characters one by one.

@echo off
setlocal enabledelayedexpansion

set "WORD=BIRD"
set "REVERSED="
set "ptr=0"

:LOOP
call set "CHAR=%%WORD:~!ptr!,1%%"
if "!CHAR!"=="" goto PRINT
set "REVERSED=!CHAR!!REVERSED!"
set /a "ptr+=1"
goto LOOP

:PRINT
echo Reversed: !REVERSED!

This method is highly efficient: it correctly stops running the loop dynamically as soon as an empty character is evaluated.

Sanitizing String Variables Properly

Before running the equality check, real-world user inputs are rarely perfect. A user inputting "Dad" must match "daD" smoothly. Furthermore, spaces should typically be ignored during sentence palindromes like "nurses run".

The Wrong Way: Direct Raw Checking

If a developer directly grabs raw user inputs and evaluates them immediately, the script will fail perfectly valid palindromes based on case sensitivity.

Wrong Code Example:

@echo off
set "WORD=Level"
set "REVERSED=leveL"

:: Direct raw comparison
if "%WORD%"=="%REVERSED%" (
echo Palindrome!
) else (
echo Not a palindrome!
)

What Happens: The code fails. The string "Level" mismatches "leveL" due to strict uppercase and lowercase evaluation differences.

The Correct Way: Unifying Case and Format

To perform a proper, clean logical check, you must strip away spaces and use the /I switch available in the IF command to ignore case completely.

Correct Code Example:

@echo off
setlocal enabledelayedexpansion

set "WORD=Race Car"

:: Remove spaces cleanly
set "CLEAN_WORD=!WORD: =!"
set "REVERSED="
set "ptr=0"

:REVERSE_LOOP
call set "CHAR=%%CLEAN_WORD:~!ptr!,1%%"
if "!CHAR!"=="" goto COMPARE
set "REVERSED=!CHAR!!REVERSED!"
set /a "ptr+=1"
goto REVERSE_LOOP

:COMPARE
:: Case insensitive string comparison using /I
if /I "!CLEAN_WORD!"=="!REVERSED!" (
echo It is a palindrome.
)

What Happens: Because spaces are safely removed, and /I ignores the case matching of "r" and "R", "Race Car" successfully registers correctly as a valid palindrome.

Full Script Implementation

By combining input sanitization, dynamic string reversal loops, and robust error checking, we construct a powerful standalone palindrome validator inside the command prompt.

@echo off
setlocal enabledelayedexpansion
title Terminal Palindrome Checker
color 0B

:PROMPT
cls
echo ========================================================
echo TERMINAL PALINDROME VALIDATOR
echo ========================================================
set "USER_TEXT="
set /p "USER_TEXT=Enter a word or phrase (or 'exit' to quit): "

if /I "!USER_TEXT!"=="exit" exit /b
if not defined USER_TEXT goto PROMPT

:: 1. Strip all spaces for sentence palindromes
set "CLEAN_TEXT=!USER_TEXT: =!"

:: 2. Reverse the string
set "REVERSED_TEXT="
set "iter=0"

:BUILD_REVERSE
call set "CURRENT_CHAR=%%CLEAN_TEXT:~!iter!,1%%"
if "!CURRENT_CHAR!"=="" goto EVALUATE
set "REVERSED_TEXT=!CURRENT_CHAR!!REVERSED_TEXT!"
set /a "iter+=1"
goto BUILD_REVERSE

:EVALUATE
echo.
echo Original: !CLEAN_TEXT!
echo Reversed: !REVERSED_TEXT!
echo --------------------------------------------------------

:: 3. Perform Case-Insensitive Comparison
if /I "!CLEAN_TEXT!"=="!REVERSED_TEXT!" (
color 0A
echo [MATCH] "!USER_TEXT!" is a valid palindrome.
) else (
color 0C
echo [NO MATCH] "!USER_TEXT!" is NOT a palindrome.
)

echo.
pause
color 0B
goto PROMPT
tip

For multi-word sentential palindromes (like "a nut for a jar of tuna"), safely removing standard punctuation (commas, periods) alongside spaces will make your script more versatile. You can chain additional substitutions such as set "CLEAN_TEXT=!CLEAN_TEXT:,=!" and set "CLEAN_TEXT=!CLEAN_TEXT:.=!" after the space removal step.

Conclusion

Creating a Palindrome Checker strictly inside Windows Batch involves cleverly employing standard variable substring slicing in iterative loops. Mastering string parsing and inversion loops functionally equips you with the fundamental skills required for dynamic memory manipulation executing in native terminal environments.