Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


palindrome


palindrome

Author
Message
sqlnewbie17
sqlnewbie17
Old Hand
Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)

Group: General Forum Members
Points: 330 Visits: 1199
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
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5679 Visits: 25280
   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 ....

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
sqlnewbie17
sqlnewbie17
Old Hand
Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)

Group: General Forum Members
Points: 330 Visits: 1199
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...
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24146 Visits: 37920
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'




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
sqlnewbie17
sqlnewbie17
Old Hand
Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)Old Hand (330 reputation)

Group: General Forum Members
Points: 330 Visits: 1199
I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanks
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24146 Visits: 37920
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?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24146 Visits: 37920
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.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24146 Visits: 37920
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';




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44766 Visits: 39845
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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44766 Visits: 39845
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

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
     Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search