PL/SQL Program to Reverse a String

Here you will get pl/sql program to reverse a string.

The substr() function returns a part of string. It has following syntax.

substr(string, position, length);

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

In this way string is reversed.

 

PL/SQL Program to Reverse a String

declare
	str1 varchar2(50):='&str';
	str2 varchar2(50);
	len number;
	i number;

begin
	len:=length(str1);
	
	for i in reverse 1..len
	loop
		str2:=str2 || substr(str1,i,1);
	end loop;
	
	dbms_output.put_line('Reverse of String is:'||str2);
end;
/

 

Output

Enter value for str: hello world
old 2: str1 varchar2(50):=’&str’;
new 2: str1 varchar2(50):=’hello world’;
Reverse of String is:dlrow olleh

2 thoughts on “PL/SQL Program to Reverse a String”

Leave a Comment

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