Created
June 25, 2022 23:04
-
-
Save PanosGreg/9b964c15ed988f200a65df3c78840ae3 to your computer and use it in GitHub Desktop.
Exercise: Reverse any letters of the input string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ReversedString { | |
<# | |
.SYNOPSIS | |
Reverse any letters of the input string | |
.EXAMPLE | |
Get-ReversedString '!abc123def456!' | |
# returns: !cba123fed456! | |
#> | |
[OutputType([string])] | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory,ValueFromPipeline)] | |
[string]$InputString | |
) | |
function ReverseList($List){$List.Reverse() ; $List ; $List.Clear()} | |
$CharArray = [char[]]$InputString | |
$TempList = [Collections.Generic.List[char]]::new() | |
$TempChars = 0..($CharArray.Length-1) | foreach { | |
$ThisChar = $CharArray[$_] | |
$IsNotEmpty = $TempList.Count -gt 0 | |
$IsChar = [char]::IsLetter($ThisChar) | |
$LastChar = $_ -eq $CharArray.Length-1 | |
if ($IsChar) {$TempList.Add($ThisChar)} | |
elseif ($IsNotEmpty) {ReverseList $TempList} | |
# reverse once more at the end if it's not empty | |
if ($LastChar -and $IsNotEmpty) {ReverseList $TempList} | |
} | |
Write-Verbose "Temp array: $($TempChars -join $null)" | |
$i = 0 | |
$ReversedArray = $CharArray | foreach { | |
$IsChar = [char]::IsLetter($_) | |
if ($IsChar) {$TempChars[$i] ; $i++} | |
else {$_} | |
} | |
$ReversedString = [string]::Join($null,$ReversedArray) | |
Write-Output $ReversedString | |
} | |
# alternative way: | |
Function Get-ReversedString ([string]$InputString) { | |
$Find = [Regex]::Matches($InputString,'[a-zA-Z]+').Value | |
$Temp = -join ($Find | ForEach {-join $_[($_.Length-1)..0]}) | |
$i = 0 | |
([char[]]$InputString | foreach { | |
$IsChar = [char]::IsLetter($_) | |
if ($IsChar) {$Temp[$i] ; $i++} | |
else {$_} | |
}) -join $null | |
} | |
# another take: | |
Function Get-ReversedString { | |
<# | |
.SYNOPSIS | |
Reverse any letters of the input string | |
.EXAMPLE | |
Get-ReversedString '!abc123def456!' | |
# returns: !cba123fed456! | |
#> | |
[OutputType([string])] | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory,ValueFromPipeline)] | |
[string]$InputString | |
) | |
$Find = [Regex]::Matches($InputString,'[a-zA-Z]+').Value | |
$Temp = -join $Find.ForEach({-join $_[($_.Length-1)..0]}) | |
$i = 0 | |
$ReversedArray = [char[]]$InputString | foreach { | |
if ([Char]::IsLetter($_)) {$Temp[$i] ; $i++} | |
else {$_} | |
} | |
Write-Output (-join $ReversedArray) | |
} | |
# and finally for PS7+ (using the ternary operator) | |
Function Get-ReversedString { | |
<# | |
.SYNOPSIS | |
Reverse any letters of the input string | |
.EXAMPLE | |
'!abc123def456!' | Get-ReversedString | |
# returns: !cba123fed456! | |
#> | |
#Requires -Version 7.0 | |
[OutputType([string])] | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory,ValueFromPipeline)] | |
[Alias('InputString')] | |
[string]$In | |
) | |
$Find = [Regex]::Matches($In,'[a-zA-Z]+').Value | |
$Temp = -join $Find.ForEach({-join $_[($_.Length-1)..0]}) | |
Write-Verbose "Temporary string: $Temp" | |
$i = 0 | |
-join ([char[]]$In).ForEach({[Char]::IsLetter($_) ? $($Temp[$i];$i++) : $_}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment