Home Forums Programming General Reverse string without built in functions RE: Reverse string without built in functions

  • pshvets (4/14/2009)


    Hmmm...

    Does it mean that recursion cannot be used in this example? Or it should be implemented differently?

    Thanks,

    Pit

    recursion has it's place, of course, but it does not serve string manipulation very well.

    without using a tally table, i'd do it like this: just a simple loop and string concatenation:

    ALTER function CharReversal(@inputstring varchar(max))

    returns varchar(max)

    WITH SCHEMABINDING

    AS

    BEGIN

    DECLARE @i int,

    @Results varchar(max)

    SET @Results=''

    SET @i = 1

    WHILE @i <= DATALENGTH(@inputstring)

    BEGIN

    SET @Results = SUBSTRING(@inputstring,@i,1) + @Results

    SET @i=@i + 1

    END

    RETURN @Results

    END

    select dbo.CharReversal('abc123xyz')

    --Results:zyx321cba

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!