Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 palindrome Rate Topic Display Mode Topic Options
Author
 Message
 Posted Friday, October 18, 2013 5:58 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Tuesday, November 19, 2013 5:53 PM Points: 170, Visits: 552
 declare @count intdeclare @charvaluefront varchardeclare @charvalueback varchardeclare @frontpos intdeclare @backpos intselect @count=len('ABC')set @frontpos=1set @backpos=@countwhile(@frontpos<=@count)beginselect @charvaluefront=substring('ABC',@frontpos,1)select @charvalueback=substring('ABC',@backpos,1)set @frontpos=@frontpos+1set @backpos=@backpos-1print @charvaluefrontprint @charvaluebackendHow 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
Post #1506345
 Posted Friday, October 18, 2013 7:54 PM
 SSCertifiable Group: General Forum Members Last Login: Today @ 12:24 PM Points: 5,333, Visits: 21,986
 ` 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)) endResults:c**Ab**BGot them its a palindrome. B ba**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. RonPlease help us, help you -before posting a question please read Before posting a performance problem please read
Post #1506355
 Posted Friday, October 18, 2013 8:24 PM
 SSC-Enthusiastic Group: General Forum Members Last Login: Tuesday, November 19, 2013 5:53 PM Points: 170, Visits: 552
 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...
Post #1506358
 Posted Saturday, October 19, 2013 3:42 AM
 SSC-Insane Group: General Forum Members Last Login: Today @ 4:10 PM Points: 22,093, Visits: 29,025
 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'`
Post #1506378
 Posted Sunday, October 20, 2013 2:03 AM
 SSC-Enthusiastic Group: General Forum Members Last Login: Tuesday, November 19, 2013 5:53 PM Points: 170, Visits: 552
 I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanks
Post #1506449
 Posted Sunday, October 20, 2013 2:39 AM
 SSC-Insane Group: General Forum Members Last Login: Today @ 4:10 PM Points: 22,093, Visits: 29,025
 sqlserver12345 (10/20/2013)I do not want to use reverse function.please check the code I posted and correct what wrong I did.thanksAnd what is wrong with using the reverse function?
Post #1506451
 Posted Sunday, October 20, 2013 3:00 AM
 SSC-Insane Group: General Forum Members Last Login: Today @ 4:10 PM Points: 22,093, Visits: 29,025
 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 = 1begin select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end; set @LoopCnt = @LoopCnt + 1;endif @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 = 1begin select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end; set @LoopCnt = @LoopCnt + 1;endif @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 = 1begin select @IsPalindrome = case when substring(@Str1,1 + @LoopCnt,1) = substring(@Str1,len(@Str1) - @LoopCnt,1) then 1 else 0 end; set @LoopCnt = @LoopCnt + 1;endif @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.
Post #1506452
 Posted Sunday, October 20, 2013 3:20 AM
 SSC-Insane Group: General Forum Members Last Login: Today @ 4:10 PM Points: 22,093, Visits: 29,025
 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;endif @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;endif @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;endif @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;endif @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;endif @IsPalindrome = 1 print @Str1 + ' is a palindrome'else print @Str1 + ' is not a palindrome';`
Post #1506454
 Posted Sunday, October 20, 2013 11:43 AM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 4:27 PM Points: 34,580, Visits: 28,763
 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". --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." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1506488
 Posted Sunday, October 20, 2013 12:13 PM
 SSC-Dedicated Group: General Forum Members Last Login: Today @ 4:27 PM Points: 34,580, Visits: 28,763
 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 AdamNone 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." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1506492

 Permissions