Search the Community
Showing results for tags 'palindrome string check'.
-
This is a quick little function to check whether or not a string is a Palindrome. Function: Optimised version. ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsPalindrome ; Description ...: Check if a string is a palindrome. ; Syntax ........: _IsPalindrome($sString) ; Parameters ....: $sString - A string value containing the palindrome. ; $bRetainNonAlphaNumeric - [optional] Retain all non-word characters. Default is False. ; Return values .: Success - True ; Failure - False ; Author ........: guinness ; Remarks .......: Characters are checked with the case ignored i.e. A == a and Z == z. ; Example .......: Yes ; =============================================================================================================================== Func _IsPalindrome($sString, $bRetainNonAlphaNumeric = Default) If Not $bRetainNonAlphaNumeric Then $sString = StringRegExpReplace($sString, '\W', '') EndIf Return $sString = StringReverse($sString) EndFunc ;==>_IsPalindromeFunction: With custom StringReverse() ; #FUNCTION# ==================================================================================================================== ; Name ..........: _IsPalindrome ; Description ...: Check if a string is a palindrome. ; Syntax ........: _IsPalindrome($sString) ; Parameters ....: $sString - A string value containing the palindrome. ; $bRetainNonAlphaNumeric - [optional] Retain all non-word characters. Default is False. ; Return values .: Success - True ; Failure - False ; Author ........: guinness ; Remarks .......: Characters are checked with the case ignored i.e. A == a and Z == z. ; Example .......: Yes ; =============================================================================================================================== Func _IsPalindrome($sString, $bRetainNonAlphaNumeric = Default) If Not $bRetainNonAlphaNumeric Then $sString = StringRegExpReplace($sString, '\W', '') EndIf Local Const $iChar_Length = 1 Local $bReturn = True, _ $i = 1, $j = StringLen($sString) While ($i < $j) ; Converted from this: http://marc.info/?l=php-general&m=120422082915762 If Not (StringMid($sString, $i, $iChar_Length) = StringMid($sString, $j, $iChar_Length)) Then $bReturn = False ExitLoop EndIf $i += 1 $j -= 1 WEnd Return $bReturn EndFunc ;==>_IsPalindromeExample use of Function: Example() Func Example() ConsoleWrite('Racecar: ' & _IsPalindrome('Racecar') & @CRLF) ConsoleWrite('String: ' & _IsPalindrome('String') & @CRLF) ConsoleWrite('A man, a plan, a canal, Panama: ' & _IsPalindrome('A man, a plan, a canal, Panama!') & @CRLF) ConsoleWrite('Palindrome: ' & _IsPalindrome('Palindrome') & @CRLF) ConsoleWrite('Was it a car or a cat I saw?: ' & _IsPalindrome('Was it a car or a cat I saw?', True) & @CRLF) ; This is a palindrome, but the flag has been set to True, so will return False, due to symbols being retained. ConsoleWrite('Elk rap song? No sparkle: ' & _IsPalindrome('Elk rap song? No sparkle') & @CRLF) ConsoleWrite('I saw desserts; I''d no lemons, alas no melon! Distressed was I: ' & _IsPalindrome('I saw desserts; I''d no lemons, alas no melon! Distressed was I') & @CRLF) EndFunc ;==>Example