October 18, 2013 at 5:58 pm
declare @count int
declare @charvaluefront varchar
declare @charvalueback varchar
declare @frontpos int
declare @backpos int
select @count=len('ABC')
set @frontpos=1
set @backpos=@count
while(@frontpos<=@count)
begin
select @charvaluefront=substring('ABC',@frontpos,1)
select @charvalueback=substring('ABC',@backpos,1)
set @frontpos=@frontpos+1
set @backpos=@backpos-1
print @charvaluefront
print @charvalueback
end
How do I compare @charvaluefront and @charvalueback values to test if the values are equal and print that its a palindrome.
I tried the if clause but it does not work.Any suggestions please
October 18, 2013 at 7:54 pm
declare @count int
declare @charvaluefront varchar
declare @charvalueback varchar
declare @frontpos int
declare @backpos int
select @count=len('ABC')
set @frontpos=1
set @backpos=@count
while(@frontpos<=@count)
begin
select @charvaluefront=substring('ABC',@frontpos,1)
select @charvalueback=substring('abc',@backpos,1)
print substring('abc',@backpos,1) + '**' + substring('ABC',@frontpos,1)
set @frontpos=@frontpos+1
set @backpos=@backpos-1
IF LTRIM(RTRIM(@charvaluefront)) = LTRIM(RTRIM(@charvalueback))
PRINT 'Got them its a palindrome. ' + LTRIM(RTRIM(@charvaluefront))+ ' ' + LTRIM(RTRIM(@charvalueback))
end
Results:
c**A
b**B
Got them its a palindrome. B b
a**C
I would suggest that you post your actual code. The IF statement works as per sample above ....
October 18, 2013 at 8:24 pm
This is my IF clause which did not work...
if (@charvaluefront= @charvalueback)
Print 'its a palindrome'
I am not sure why it did not work...
October 19, 2013 at 3:42 am
What are you trying to accomplish?
declare @Str1 varchar(10) = 'ABBA';
if @Str1 = reverse( @Str1)
print @Str1 + ' is a Palindrome'
else
print @Str1 + ' is not a Palindrome'
set @Str1 = 'ABC';
if @Str1 = reverse( @Str1)
print @Str1 + ' is a Palindrome'
else
print @Str1 + ' is not a Palindrome'
October 20, 2013 at 2:03 am
I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanks
October 20, 2013 at 2:39 am
sqlserver12345 (10/20/2013)
I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanks
And what is wrong with using the reverse function?
October 20, 2013 at 3:00 am
And if you must use a loop then how about this?
declare @Str1 varchar(10) = 'ABBA';
declare @IsPalindrome bit = 1;
declare @LoopCnt int = 0;
while @LoopCnt < len(@Str1) and @IsPalindrome = 1
begin
select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end;
set @LoopCnt = @LoopCnt + 1;
end
if @IsPalindrome = 1
print @Str1 + ' is a palindrome'
else
print @Str1 + ' is not a palindrome'
set @LoopCnt = 0;
set @IsPalindrome = 1;
set @Str1 = 'ABC';
while @LoopCnt < len(@Str1) and @IsPalindrome = 1
begin
select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end;
set @LoopCnt = @LoopCnt + 1;
end
if @IsPalindrome = 1
print @Str1 + ' is a palindrome'
else
print @Str1 + ' is not a palindrome'
set @LoopCnt = 0;
set @IsPalindrome = 1;
set @Str1 = 'ABA';
while @LoopCnt < len(@Str1) and @IsPalindrome = 1
begin
select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end;
set @LoopCnt = @LoopCnt + 1;
end
if @IsPalindrome = 1
print @Str1 + ' is a palindrome'
else
print @Str1 + ' is not a palindrome'
You will also notice that I short circuit my loop to avoid unnecessary comparisons.
October 20, 2013 at 3:20 am
Short circuited the loop even more:
declare @Str1 varchar(10) = 'ABBA';
declare @IsPalindrome bit = 1;
declare @LoopCnt int = 0;
while @LoopCnt < len(@Str1) and @IsPalindrome = 1 and (1 + @LoopCnt < len(@Str1) - @LoopCnt)
begin
select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end;
set @LoopCnt = @LoopCnt + 1;
end
if @IsPalindrome = 1
print @Str1 + ' is a palindrome'
else
print @Str1 + ' is not a palindrome';
set @LoopCnt = 0;
set @IsPalindrome = 1;
set @Str1 = 'ABC';
while @LoopCnt < len(@Str1) and @IsPalindrome = 1 and (1 + @LoopCnt < len(@Str1) - @LoopCnt)
begin
select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end;
set @LoopCnt = @LoopCnt + 1;
end
if @IsPalindrome = 1
print @Str1 + ' is a palindrome'
else
print @Str1 + ' is not a palindrome';
set @LoopCnt = 0;
set @IsPalindrome = 1;
set @Str1 = 'ABA';
while @LoopCnt < len(@Str1) and @IsPalindrome = 1 and (1 + @LoopCnt < len(@Str1) - @LoopCnt)
begin
select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end;
set @LoopCnt = @LoopCnt + 1;
end
if @IsPalindrome = 1
print @Str1 + ' is a palindrome'
else
print @Str1 + ' is not a palindrome';
set @LoopCnt = 0;
set @IsPalindrome = 1;
set @Str1 = 'ABACDCABA';
while @LoopCnt < len(@Str1) and @IsPalindrome = 1 and (1 + @LoopCnt < len(@Str1) - @LoopCnt)
begin
select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end;
set @LoopCnt = @LoopCnt + 1;
end
if @IsPalindrome = 1
print @Str1 + ' is a palindrome'
else
print @Str1 + ' is not a palindrome';
set @LoopCnt = 0;
set @IsPalindrome = 1;
set @Str1 = 'ABACDDCABA';
while @LoopCnt < len(@Str1) and @IsPalindrome = 1 and (1 + @LoopCnt < len(@Str1) - @LoopCnt)
begin
select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end;
set @LoopCnt = @LoopCnt + 1;
end
if @IsPalindrome = 1
print @Str1 + ' is a palindrome'
else
print @Str1 + ' is not a palindrome';
October 20, 2013 at 11:43 am
sqlserver12345 (10/20/2013)
I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanks
I'm curious... please explain why you don't want to use "REVERSE".
--Jeff Moden
Change is inevitable... Change for the better is not.
October 20, 2013 at 12:13 pm
sqlserver12345 (10/20/2013)
I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanks
I'm curious... please explain why you don't want to use "REVERSE".
I also have to ask what your intended definition of a "palindrome" actually is. For example, the following is a palindrome.
Madam I'm Adam
None of the solutions so far take into account any punctuation or spaces.
Also, do you consider a single letter to be a palindrome?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 20, 2013 at 1:36 pm
Jeff Moden (10/20/2013)
sqlserver12345 (10/20/2013)
I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanksI'm curious... please explain why you don't want to use "REVERSE".
I also have to ask what your intended definition of a "palindrome" actually is. For example, the following is a palindrome.
Madam I'm Adam
None of the solutions so far take into account any punctuation or spaces.
Also, do you consider a single letter to be a palindrome?
I never thought about taking out punctuation or spaces. Shouldn't be that hard to implement, just not going to do the additional work. I am curious why he doesn't want to use REVERSE either. I haven't seen a response to my question on that one either.
October 20, 2013 at 2:11 pm
The 'no reverse' is an interesting requirement. Here's my attempt at it
with stringList as (
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ID, String,
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(String,' ',''),'.',''),',',''),'''',''),'-','') Compact --More replaces are likely
FROM (VALUES ('bob'),('Madam, I''m Adam'),('noon'),('fail'),('a')) AS I(String)
),
cteTally as (
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) N
FROM (VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t1(N),
(VALUES (0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) t2(N)
)
SELECT ID, String, CASE WHEN SUM(c) = LEN(Compact)/2 THEN 'Palidrome' ELSE 'Not Palidrome' END C
FROM (
SELECT ID, String, Compact, CASE WHEN SUBSTRING(Compact,N,1) = SUBSTRING(Compact,LEN(Compact) - N + 1, 1) THEN 1 ELSE 0 END c
FROM stringLists
OUTER APPLY (SELECT TOP(LEN(Compact)/2) N FROM cteTally) t
) a
GROUP BY ID, String, Compact;
October 20, 2013 at 5:01 pm
mickyT (10/20/2013)
The 'no reverse' is an interesting requirement.
I believe what we have here is homework about how to use WHILE loops and the instructor probably never heard of a Tally or Numbers table. Although it's good for people in the tuning business, I wish instructors would get a clue about such "pseudo cursors".
--Jeff Moden
Change is inevitable... Change for the better is not.
October 20, 2013 at 5:38 pm
sqlserver12345 (10/18/2013)
declare @count intdeclare @charvaluefront varchar
declare @charvalueback varchar
declare @frontpos int
declare @backpos int
select @count=len('ABC')
set @frontpos=1
set @backpos=@count
while(@frontpos<=@count)
begin
select @charvaluefront=substring('ABC',@frontpos,1)
select @charvalueback=substring('ABC',@backpos,1)
set @frontpos=@frontpos+1
set @backpos=@backpos-1
print @charvaluefront
print @charvalueback
end
How do I compare @charvaluefront and @charvalueback values to test if the values are equal and print that its a palindrome.
I tried the if clause but it does not work.Any suggestions please
Ok, the more I think about this and the "No REVERSE" requirement, the more I think this is homework and the instructor wants you to use WHILE loops. Are you being required to use a WHILE loop or not because, in real life, I wouldn't go anywhere near a WHILE loop for this. Of course, in real life, I'd be using a REVERSE after I cleaned the string.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 20, 2013 at 5:55 pm
Jeff Moden (10/20/2013)
mickyT (10/20/2013)
The 'no reverse' is an interesting requirement.I believe what we have here is homework about how to use WHILE loops and the instructor probably never heard of a Tally or Numbers table. Although it's good for people in the tuning business, I wish instructors would get a clue about such "pseudo cursors".
🙂 I suspected the same, you don't usually strike such restrictions in real life:-) It does however provide some good little brain stretchers occasionally.
Viewing 15 posts - 1 through 15 (of 20 total)
You must be logged in to reply to this topic. Login to reply