PL/SQL Program to Reverse a String

PL/SQL Program to Reverse a String


Here we are going to write a pl/sql program to reverse a string.

In this below program going to use the substr() function. substr() function returns a part of string.

Syntax of substr() function is :-

substr(string, position, length);

We are going to use this function to extract character by character from string in reverse order and concatenate it previous extracted character. || is used to concatenate string.


PL/SQL Program to Reverse a String

declare
  string1 varchar2(50):='amazon';
  string2 varchar2(50);
  len number;
  k number;
 
begin
  len:=length(string1);
  
  for k in reverse 1..len
  loop
    string2:=string2 || substr(string1,k,1);
  end loop;
  
  dbms_output.put_line('String After Reverse is:'||string2);
end;

Output is

String After Reverse is:nozama

Leave a Reply

Your email address will not be published. Required fields are marked *