forked from abhishekdoifode1/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrom.ps1
35 lines (34 loc) · 990 Bytes
/
palindrom.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Function Test–IsPalindrome {
[cmdletbinding()]
Param(
[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True)]
[ValidateNotNullorEmpty()]
[string]$Text,
[switch]$IgnoreSpace
)
Process {
Write-Verbose "Testing $Text"
if ($IgnoreSpace) {
Write-Verbose "Removing spaces from text"
$text = $text.Replace(" ","")
Write-Verbose $text
}
$l = $text.length
Write-Verbose "Length is $l"
$mid = [math]::Truncate($l/2)
Write-Verbose "Midpoint is $mid"
# I love Naruto
#could also use Substring()
$start = –join ($text[0..($mid–1)]) #$text.Substring(0,($mid))
$end = –join ($text[–1..–($mid)])
Write-Verbose "Start: $start"
Write-Verbose "Pivot: $($Text[$mid])"
Write-Verbose "End : $end"
if ($start -eq $end) {
$True
}
else {
$false
}
}
}