Reverse a string in PL/SQL
Last Updated :
29 Jun, 2018
Prerequisite – PL/SQL introduction
In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations.
Given a string, the task is to reverse a string using PL/SQL.
Examples:
Input: skeegrofskeeg
Output: geeksforgeeks
Input: geeks
Output: skeeg
Approach:
- Find the length of the string.
- Then traverse the string in a reverse manner.
- Store the characters in another string.
- Print the final string.
Below is the required implementation:
DECLARE
str VARCHAR (20) := 'skeegrofskeeg' ;
len NUMBER;
str1 VARCHAR (20);
BEGIN
len := Length(str);
FOR i IN REVERSE 1.. len LOOP
str1 := str1
|| Substr(str, i, 1);
END LOOP;
dbms_output.Put_line( 'Reverse of string is '
|| str1);
END ;
|
Output :
Reverse of string is geeksforgeeks
Share your thoughts in the comments
Please Login to comment...