Use PowerShell to Find Palindromes
Summary: Ed Wilson, Microsoft Scripting Guy, talks about using Windows PowerShell to find palindromes.
Microsoft Scripting Guy, Ed Wilson, is here. I enjoy playing around with words. I like words that sound similar to each other, such as to, too, and two. I also like words that look alike, and sound alike but have different meanings depending on the context, such as might and might. Of course, there is also mite, which goes back into the previous category of words that sound alike. There are also words that look alike, but are pronounced differently depending on the context (which also, of course, alters the meaning), such as tear and tear or dove and dove.
One of my favorites is called the palindrome. That is a word that reads the same forward and backward. A couple of examples of palindromes that are easy to see are mom and dad. A palindrome that might not be quite so obvious is racecar.
How can I tell if a word is a palindrome without going cross-eyed staring at it and trying to count forwards and backwards? Easy. I can use Windows PowerShell.
Identifying a palindrome
There are really only two things that I need to do to identify a palindrome:
- Flip the word.
- Compare the flipped (reversed) letters with the original.
The steps that I need to take to satisfy these tasks are:
- Store the original word in a variable.
- Reverse the original word by splitting it into an array and storing it in a different variable.
- Join the reversed array back into a word.
- Compare the original variable with the new variable containing the reversed word.
Developing a proof-of-concept
To see if I am on the right track, and to have code that is relatively easy to read, I decided to write a simple bit of proof-of-concept code. This is not finished code, it has no error checking in it, it requires manual editing to use, and all of that stuff. But it does illustrate the technique that I need to use.
The first thing I do is store my value in a variable. I then create a second variable that holds the word as an array:
$a = 'wilber'
$b = $a.ToCharArray()
Now I reverse the array by calling the static reverse method from the [array] class. When I do this, the reversed array is automatically stored back into the variable that contains the array, and nothing is output. This line of code is shown here:
Now I need to join the array back into a string. To do this, I use the –join operator. I store the re-created/reversed string into a new variable:
$c = -join($b)
Note All of the previous techniques were discussed in the Hey, Scripting Guy! Blog post, Reverse Strings with PowerShell.
Because this is proof-of-concept code, and I will not be automating it, I decide to display a string that tells me what is happening. I escape the colon with the grave accent ( ` ), and I include a return and a new line character:
"$a equals $c`:`r`n—–"
I then do an equality operation to see if the value in $a is equal to the value in $c. If it is, I have a palindrome. If not, I do not have one. The following image shows the output from testing dad and wilber:
That is all there is to using Windows PowerShell to identify palindromes. Join me tomorrow when I will talk about more cool stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy