December 21, 2007 at 2:51 am
Hi Experts,
I want to know whether a string stored in a table is palindrome or not.
for example,
Table: Emp
Eno Ename Status
1 Karthik
2 MADAM
3 Matt
4 Steve
5 LEVEL
6 Jhone
7 LIRIL
8 Toner
9 ROTATOR
10 Gila
I want to update the status to 'PALINDROME' (or) 'NOT PALINDROME' based on the letter format.(I hope all our members knows about Palindrome)
Thanks in advance.
karthik
December 21, 2007 at 2:56 am
UPDATE Table1
SET Status = CASE WHEN Col1 = REVERSE(Col1) THEN 1 ELSE 0 END
N 56°04'39.16"
E 12°55'05.25"
December 21, 2007 at 2:57 am
UPDATE Emp
SET Status = CASE WHEN Ename = REVERSE(Ename) THEN 'Palindrome' ELSE ' No palindrome' END
N 56°04'39.16"
E 12°55'05.25"
December 21, 2007 at 3:40 am
I dont want to use REVERSE() Function. I need pure sql code logic which could check whether it is palindrome or not.
karthik
December 21, 2007 at 3:46 am
Why don't you want to use REVERSE? Is this a homework question?
John
December 21, 2007 at 3:51 am
We can use REVERSE() Function.I agreed.
But my friend faced the above question in his recent interview.Interviewer asked him not to use REVERSE() Function.But he failed to answer. Thats why i mentioned.
karthik
December 21, 2007 at 4:29 am
Karthik
Try this.
John
DECLARE @word varchar(20)
DECLARE @length smallint
SET @word = 'AbleWasIEreISawElba'
SET @length = LEN(@word)
IF EXISTS (
SELECT *
FROM master.dbo.spt_values
WHERE type = 'P'
AND number between 1 and @length/2.0 + 1
AND SUBSTRING(@word,number,1) <> SUBSTRING(@word,@length-number+1,1)
)
PRINT 'Not palindrome'
ELSE PRINT 'Palindrome'
December 21, 2007 at 4:44 am
John,
Can you explain me the logic ?
karthik
December 21, 2007 at 4:47 am
C'mon, Karthik - do some of the work yourself! What don't you understand?
John
December 21, 2007 at 5:09 am
@length/2.0 + 1
I am not able to understand the above statement.Why are you dividing it by 2 and add with 1.
karthik
December 21, 2007 at 5:19 am
Karthik
We're checking that the first character is equal to the last, the second is equal to the second to last, and so on. You only need to do that for the first n characters of the word, where n is length/2 if there are an even number of characters, or length/2 + 1 if odd. As an example, if there are 10 characters, it does it where number < 6 (ie 5 times), and if there are 11 characters it does it where number < 6.5 (ie 6 times). (In fact, it really only needs to do it (length-1)/2 times, since the middle character will always be equal to the middle character.) The reason for dividing by 2.0 instead of 2 is that this gives you a result accurate to one decimal place, instead of just an integer.
Hope that helps
John
December 21, 2007 at 11:36 pm
Karthik,
This is what I spoke of on one of the other posts... it's also why people get so angry with you. You banter the "Senior Software Engineer" label all over the place, but you do not think like one. Take some time and analyze the code instead of asking what the logic is... it'll take you longer, but you'll learn more... a lot more.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 28, 2007 at 12:13 am
karthikeyan (12/21/2007)
@length/2.0 + 1I am not able to understand the above statement.Why are you dividing it by 2 and add with 1.
Are you a front-end developer or back-end developer?
What do you do in your organisation?
Failing to plan is Planning to fail
December 28, 2007 at 12:15 am
Jeff Moden (12/21/2007)
Karthik,This is what I spoke of on one of the other posts... it's also why people get so angry with you. You banter the "Senior Software Engineer" label all over the place, but you do not think like one. Take some time and analyze the code instead of asking what the logic is... it'll take you longer, but you'll learn more... a lot more.
I agree with you
Failing to plan is Planning to fail
December 28, 2007 at 12:32 am
I think some times people expect the FORUMS to give them the FINE results which saves them all the work. It looks like they dont want to learn fishing, but just want to get a fish when they need it.
It happened to me in one of the MSDN forums that I asked the user some clarification about the problem while trying to help him out. He replied in rude (I guess that is what a message in UPPER CASE letters mean) which said something like "IF YOU HAVE READ MY POST CAREFULLY ......". The message looked like he is very busy and does not have enough time to explain his problem clearly. But he expected some one to provide a clean solution without he being involved.
🙂
.
Viewing 15 posts - 1 through 15 (of 15 total)
You must be logged in to reply to this topic. Login to reply