Writing a program to reverse a string (for example, turning "hello" into "olleh") is a classic programming challenge. It's a great way to learn how to use loops and string functions to manipulate data one character at a time.
This simple guide will show you the logic and a complete PL/SQL program to solve this problem.
What You Need to Know
To write this program, you will use a few basic PL/SQL concepts:
- Enabling Output: You must run this command once in your SQL tool (like SQL*Plus or SQL Developer) to see the printed results:
SET SERVEROUTPUT ON; - Anonymous Block: We will write our code in a
DECLARE...BEGIN...END;block. - Variables: We'll need a variable for the original string (
v_string), a variable to hold the new reversed string (v_reversed_string), and a variable for the string's length (v_length). LENGTH(string): This built-in function tells us how many characters are in the string.SUBSTR(string, position, length): This function extracts a part of a string. We will useSUBSTR(v_string, i, 1)to get the single character at positioni.FORLoop withREVERSE: We will use aFORloop to iterate through the string. TheREVERSEkeyword makes the loop count backward (e.g., from 10 down to 1), which is perfect for this task.- Concatenation (
||): We use the||operator to build the new string by appending one character at a time.
PL/SQL Program: Reverse a String
This program will take the string stored in v_string, reverse it, and print the result.
PL/SQL Program
SET SERVEROUTPUT ON;
DECLARE
-- The string we want to reverse
v_string VARCHAR2(100) := 'Hello World';
-- A variable to hold the new string as we build it
v_reversed_string VARCHAR2(100);
-- A variable to store the length
v_length NUMBER;
BEGIN
-- 1. Get the length of the string
v_length := LENGTH(v_string);
-- 2. Loop from the end of the string down to the beginning
FOR i IN REVERSE 1..v_length LOOP
-- 3. Get the character at the current position 'i'
-- and append it to our new string
v_reversed_string := v_reversed_string || SUBSTR(v_string, i, 1);
END LOOP;
-- 4. Print the final results
DBMS_OUTPUT.PUT_LINE('Original string: ' || v_string);
DBMS_OUTPUT.PUT_LINE('Reversed string: ' || v_reversed_string);
END;
/
Result (for v_string := 'Hello World')
Original string: Hello World
Reversed string: dlroW olleH
Program Explanation
DECLAREsection: We createv_string('Hello World'),v_reversed_string(which isNULLat the start), andv_length.BEGINsection: The logic starts.v_length := LENGTH(v_string);: This calculates the length of 'Hello World' and stores11in thev_lengthvariable.FOR i IN REVERSE 1..v_length LOOP: This is our loop. Because we usedREVERSEandv_lengthis 11, the loop will run withitaking the values:11,10,9,8,7,6,5,4,3,2, and finally1.v_reversed_string := v_reversed_string || SUBSTR(v_string, i, 1);: This is where the reversed string is built.- Loop 1 (i=11):
v_reversed_string=NULL || SUBSTR('Hello World', 11, 1)= 'd' - Loop 2 (i=10):
v_reversed_string='d' || SUBSTR('Hello World', 10, 1)= 'dl' - Loop 3 (i=9):
v_reversed_string='dl' || SUBSTR('Hello World', 9, 1)= 'dlr' - ...This continues, appending each character from the end of the original string to the new string.
- Loop 11 (i=1):
v_reversed_string='dlroW olle' || SUBSTR('Hello World', 1, 1)= 'dlroW olleH'
- Loop 1 (i=11):
END LOOP;: The loop finishes.DBMS_OUTPUT.PUT_LINE(...): The program prints the original string and the final value ofv_reversed_string.
