1. The Forum has been upgraded to a modern and advanced system. Please read the announcement about this update.
  2. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

SQL for String Palindrome

Discussion in 'SQL PL/SQL' started by jakirajam, Feb 24, 2010.

  1. jakirajam

    jakirajam Active Member

    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    55
    Code (SQL):
    DECLARE
       v_name varchar2(30):='&give_name';
         v_parse varchar2(50):=NULL;
                  BEGIN
          dbms_output.put_line('the given nameis'||v_name);
               FOR x IN 1..LENGTH(v_name)
                 loop
         v_parse:=v_parse||substr(v_name,x,1);
           dbms_output.put_line('the position of the string is'||v_parse);
                     END loop;
                v_parse:=NULL;              
                 FOR Y IN reverse 1..LENGTH(v_name)
                          loop
            v_parse:= v_parse||substr(v_name,LENGTH(v_name)-1,1);
          dbms_output.put_line('the position of the string is'||v_parse);
             
                         END loop;
                           END;
    /
     
  2. zargon

    zargon Community Moderator Forum Guru

    Messages:
    2,143
    Likes Received:
    309
    Trophy Points:
    1,430
    Location:
    Aurora, CO
    Re: iam giving string in this but iam not getting palidrom ,my loop is not working

    Obviously, since length(v_name) - 1 is always the same character. You need to write your code this way:

    Code (SQL):
    DECLARE
    v_name varchar2(30):='&give_name';
    v_parse varchar2(50):=NULL;
    v_currpos NUMBER:=LENGTH(v_name);
    BEGIN
    dbms_output.put_line('the given name is '||v_name);
    FOR x IN 1..LENGTH(v_name)
    loop
    v_parse:=v_parse||substr(v_name,x,1);
    dbms_output.put_line('the position of the string is '||v_parse);
    END loop;
    v_parse:=NULL;
    FOR Y IN reverse 1..LENGTH(v_name)
    loop
    v_parse:= v_parse||substr(v_name,v_currpos,1);
    v_currpos:=v_currpos - 1;
    dbms_output.put_line('the position of the string is '||v_parse);

    END loop;
    END;
    /
    and you'll see the desireed output:

    the given name is nord
    the position of the string is n
    the position of the string is no
    the position of the string is nor
    the position of the string is nord
    the position of the string is d
    the position of the string is dr
    the position of the string is dro
    the position of the string is dron
     
  3. rajavu

    rajavu Forum Guru

    Messages:
    815
    Likes Received:
    52
    Trophy Points:
    610
    Location:
    @ Bangalore , India
    Another way of doing in straight SQL.


    Code (SQL):


    SQL> WITH str AS ( SELECT UPPER('&str') s FROM dual )
      2  SELECT 'Given word is ' ||s||'; Its reverse is '|| reverse(s) ||
      3          '; So it is '||DECODE(s,reverse(s),'PALIANDROME','NOT PALIANDROME'

      4          RESULT
      5  FROM str ;
    Enter VALUE FOR str: english
    OLD   1: WITH str AS ( SELECT UPPER('&str') s FROM dual )
    NEW   1: WITH str AS ( SELECT UPPER('english') s FROM dual )

    RESULT
    -----------------------------------------------------------------------
    Given word IS ENGLISH; Its reverse IS HSILGNE; So it IS NOT PALIANDROME

    SQL>
    SQL> /
    Enter VALUE FOR str: malayalam
    OLD   1: WITH str AS ( SELECT UPPER('&str') s FROM dual )
    NEW   1: WITH str AS ( SELECT UPPER('malayalam') s FROM dual )

    RESULT
    -----------------------------------------------------------------------
    Given word IS MALAYALAM; Its reverse IS MALAYALAM; So it IS PALIANDROME

    SQL>
     
     
  4. zargon

    zargon Community Moderator Forum Guru

    Messages:
    2,143
    Likes Received:
    309
    Trophy Points:
    1,430
    Location:
    Aurora, CO
    I believe jakirajam misspoke and simjply needed to code a PL/SQL loop to reverse a string. REVERSE() is a handy function, true, but it appears the PL/SQL assignment was a learning exercise.
     
  5. rajavu

    rajavu Forum Guru

    Messages:
    815
    Likes Received:
    52
    Trophy Points:
    610
    Location:
    @ Bangalore , India
    yeah.. Zargin might be right.

    Anyway no need for additional v_currpos variable. we can use the 'y' instead. Note the use of

    Code (SQL):
    v_parse:= v_parse||substr(v_name,y,1);



    Code (SQL):

    SQL> DECLARE
      2  v_name varchar2(30):='&give_name';
      3  v_parse varchar2(50):=NULL;
      4  BEGIN
      5  dbms_output.put_line('the given name is '||v_name);
      6  FOR x IN 1..LENGTH(v_name)
      7  loop
      8  v_parse:=v_parse||substr(v_name,x,1);
      9  dbms_output.put_line('the position of the string is '||v_parse);
     10  END loop;
     11  v_parse:=NULL;
     12  FOR Y IN reverse 1..LENGTH(v_name)
     13  loop
     14  v_parse:= v_parse||substr(v_name,y,1);
     15  dbms_output.put_line('the position of the string is '||v_parse);
     16  END loop;
     17  END;
     18  /
    Enter VALUE FOR give_name: zargon
    OLD   2: v_name varchar2(30):='&give_name';
    NEW   2: v_name varchar2(30):='zargon';
    the given name IS zargon
    the POSITION OF the string IS z
    the POSITION OF the string IS za
    the POSITION OF the string IS zar
    the POSITION OF the string IS zarg
    the POSITION OF the string IS zargo
    the POSITION OF the string IS zargon
    the POSITION OF the string IS n
    the POSITION OF the string IS no
    the POSITION OF the string IS nog
    the POSITION OF the string IS nogr
    the POSITION OF the string IS nogra
    the POSITION OF the string IS nograz

    PL/SQL PROCEDURE successfully completed.

    SQL>