Skip to content

Commit 27f93ff

Browse files
committed
Add script
1 parent 180a10d commit 27f93ff

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

General/Test-IsPalindrome.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function Test-IsPalindrome {
2+
<#
3+
.SYNOPSIS
4+
Test a string to see if it is a palindrome.
5+
6+
.DESCRIPTION
7+
This function tests a string to see if it is a palindrome. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.
8+
9+
.PARAMETER String
10+
The string to test.
11+
12+
.EXAMPLE
13+
Test-IsPalindrome -String "racecar"
14+
15+
The string "racecar" is a palindrome, so the function returns $true.
16+
17+
.EXAMPLE
18+
'() ()' | Test-IsPalindrome
19+
20+
The string "() ()" is not a palindrome, so the function returns $false.
21+
22+
.EXAMPLE
23+
'() )(' | Test-IsPalindrome
24+
25+
The string "() )(" is a palindrome, so the function returns $true.
26+
#>
27+
[CmdletBinding()]
28+
[OutputType([bool])]
29+
param (
30+
# The string to test
31+
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
32+
[ValidateNotNullOrEmpty()]
33+
[string]
34+
$String
35+
)
36+
37+
process {
38+
$ReversedString = -join ($String.ToLower().ToCharArray() | ForEach-Object { $_ })[-1.. - ($String.Length)]
39+
"`n$String || $ReversedString" | Write-Verbose
40+
$Result = $String -eq $ReversedString
41+
}
42+
43+
end {
44+
$Result
45+
}
46+
}

0 commit comments

Comments
 (0)