Rss Feed Like Us on facebook Google Plus

March 8, 2013

Check String is Palindrome or Not using T-Sql Script +Reverse Funtion


The palindrome is a word, phrase, or sequence that reads the same backward as forward. 

"A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction."

For example  
A man, a plan, a canal – Panama! is palindrome 
so as Was it a car or a cat I saw?

Palindrome in Sanskrit --


सारस नयना घन जघ
नारचित रतार कलिक हर सार रसा
सार रसारह कलिकर
तारत चिरनाघ जनघ नायनसरसा |
SQL Script



DECLARE @PalinString VARCHAR(256);
SET @PalinString = 'सारस नयना घन जघ नारचित रतार कलिक हर सार रसा सार रसारह कलिकर तारत चिरनाघ जनघ नायनसरसा';
SELECT CASE WHEN REPLACE(@PalinString, ' ', '') = REVERSE(REPLACE(@PalinString, ' ', ''))
THEN 'Palindrome'ELSE 'Not Palindrome' END AS [Answer]
GO
 

Sql Script - "Was it a car or a cat I saw"

DECLARE @PalinString VARCHAR(256);
SET @PalinString = 'Was it a car or a cat I saw';SELECT CASE WHEN REPLACE(@PalinString, ' ', '') =REVERSE(REPLACE(@PalinString, ' ', ''))THEN 'Palindrome'ELSE 'Not Palindrome' END AS [Answer]
GO
 


Again, if the word is not Palindrome you can just will get answered as it is not a palindrome. My script currently is removing all the spaces from the string. However, if your string is like A man, a plan, a canal – Panama! you may have to remove the exclamation mark and comma too using the REPLACE function.

Sql Script - " A man, a plan, a canal – Panama! "



DECLARE @PalinString VARCHAR(256);
SET @PalinString = 'A man, a plan, a canal - Panama!';
SELECT CASE WHEN REPLACE(REPLACE(REPLACE(REPLACE(@PalinString, '-',''), '!',''), ',',''), ' ', '')
= REVERSE(REPLACE(REPLACE(REPLACE(REPLACE(@PalinString, '-',''), '!',''), ',',''), ' ', ''))
THEN 'Palindrome'
ELSE 'Not Palindrome' END AS [Answer]
GO

© 2011-2016 Techimpulsion All Rights Reserved.


The content is copyrighted to Tech Impulsion and may not be reproduced on other websites.