SQL for String Palindrome

in Oracle Database; Code sql : DECLARE v_name varchar2 ( 30 ) : = '&give_name' ; v_parse varchar2 ( 50 ) : = ...
+ Post Reply + Post New Topic
Results 1 to 5 of 5
  1. #1
    jakirajam is offline Junior Member
    Points: 310, Level: 6
    Join Date
    29 Jan 2010
    Posts
    9
    Points
    310

    Lightbulb SQL for String Palindrome

    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. #2
    zargon's Avatar
    zargon is offline Forum Guru
    Points: 26,370, Level: 97
    Join Date
    22 Oct 2008
    Location
    Aurora, CO
    Posts
    2,022
    Points
    26,370

    Re: iam giving string in this but iam not getting palidrom ,my loop is not working

    Quote Originally Posted by jakirajam View Post
    declare
    v_name varchar2(30):='&give_name';
    v_parse varchar2(50):=null;
    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,length(v_name)-1,1);
    dbms_output.put_line('the position of the string is'||v_parse);

    end loop;
    end;
    /
    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
    Last edited by zargon; 02-24-2010 at 04:48 PM.

  3. #3
    rajavu's Avatar
    rajavu is offline Forum Guru
    Points: 10,830, Level: 68
    Join Date
    13 Oct 2008
    Location
    @ Bangalore , India
    Posts
    815
    Points
    10,830

    Re: SQL for String Palindrome

    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>

    Raj.

  4. #4
    zargon's Avatar
    zargon is offline Forum Guru
    Points: 26,370, Level: 97
    Join Date
    22 Oct 2008
    Location
    Aurora, CO
    Posts
    2,022
    Points
    26,370

    Re: SQL for String Palindrome

    Quote Originally Posted by rajavu View Post
    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>
    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. #5
    rajavu's Avatar
    rajavu is offline Forum Guru
    Points: 10,830, Level: 68
    Join Date
    13 Oct 2008
    Location
    @ Bangalore , India
    Posts
    815
    Points
    10,830

    Re: SQL for String Palindrome

    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>

    Raj.

Other Solutions
  1. Number to string
    By vibecy in forum Oracle Webcenter Suite (formerly Oracle ECM)
    Replies: 2
    Last Post: 08-31-2010, 01:04 PM
  2. Replacing more then one String with null
    By Tariq Bashir Malhi in forum SQL PL/SQL
    Replies: 1
    Last Post: 06-22-2010, 07:06 AM
  3. how to find string length of size 100 kb
    By laxman in forum SQL PL/SQL
    Replies: 2
    Last Post: 04-30-2010, 10:11 AM
  4. string comparison issue reg
    By laxman in forum SQL PL/SQL
    Replies: 7
    Last Post: 02-23-2010, 01:22 PM
  5. Help me with a string
    By DannyELuSh in forum Oracle Forms and Reports
    Replies: 1
    Last Post: 10-28-2009, 07:53 AM