PowerShell Script to Check Palindrome String
Use
$input -eq -join ($input.ToCharArray()[-1..-($input.Length)]) in PowerShell to check if a string is a palindrome by comparing it to its reversed version.Examples
Inputmadam
Outputmadam is a palindrome.
Inputhello
Outputhello is not a palindrome.
InputA
OutputA is a palindrome.
How to Think About It
To check if a string is a palindrome, compare the original string with its reversed version. If both are the same, the string reads the same forwards and backwards, so it is a palindrome.
Algorithm
1
Get the input string.2
Convert the string into an array of characters.3
Reverse the array of characters.4
Join the reversed characters back into a string.5
Compare the original string with the reversed string.6
Return whether the string is a palindrome based on the comparison.Code
powershell
function Test-Palindrome { param([string]$input) $reversed = -join ($input.ToCharArray()[-1..-($input.Length)]) if ($input -eq $reversed) { Write-Output "$input is a palindrome." } else { Write-Output "$input is not a palindrome." } } Test-Palindrome -input "madam" Test-Palindrome -input "hello" Test-Palindrome -input "A"
Output
madam is a palindrome.
hello is not a palindrome.
A is a palindrome.
Dry Run
Let's trace the input 'madam' through the code
1
Input string
input = 'madam'
2
Reverse string
reversed = 'madam'
3
Compare original and reversed
'madam' -eq 'madam' is True
| Step | Original String | Reversed String | Is Palindrome? |
|---|---|---|---|
| 1 | madam | madam | True |
Why This Works
Step 1: Convert string to array
The string is split into characters using ToCharArray() to allow reversing.
Step 2: Reverse the array
Using array slicing [-1..-length] reverses the character order.
Step 3: Compare strings
The original string is compared to the reversed string with -eq to check palindrome.
Alternative Approaches
Using [string]::Join and Array Reverse
powershell
function Test-PalindromeAlt { param([string]$input) $chars = $input.ToCharArray() [array]::Reverse($chars) $reversed = [string]::Join('', $chars) if ($input -eq $reversed) { Write-Output "$input is a palindrome." } else { Write-Output "$input is not a palindrome." } } Test-PalindromeAlt -input "madam"
This method explicitly reverses the array using .NET method, which may be clearer but slightly longer.
Using a loop to build reversed string
powershell
function Test-PalindromeLoop { param([string]$input) $reversed = '' for ($i = $input.Length - 1; $i -ge 0; $i--) { $reversed += $input[$i] } if ($input -eq $reversed) { Write-Output "$input is a palindrome." } else { Write-Output "$input is not a palindrome." } } Test-PalindromeLoop -input "madam"
This approach uses a manual loop to reverse the string, which is more verbose but easy to understand.
Complexity: O(n) time, O(n) space
Time Complexity
The script processes each character once to reverse the string, so time grows linearly with string length.
Space Complexity
A new array for reversed characters is created, so space also grows linearly with input size.
Which Approach is Fastest?
Using array slicing is concise and efficient; manual loops are slower but clearer for beginners.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Array slicing with -join | O(n) | O(n) | Concise and fast for typical use |
| [array]::Reverse method | O(n) | O(n) | Clear .NET method usage |
| Manual loop reversal | O(n) | O(n) | Educational clarity, less concise |
Use
-join ($string.ToCharArray()[-1..-($string.Length)]) to quickly reverse a string in PowerShell.Beginners often forget to join the reversed character array back into a string before comparing.