PL/SQL Program to Reverse a String

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:

  1. 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;
  2. Anonymous Block: We will write our code in a DECLARE...BEGIN...END; block.
  3. 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).
  4. LENGTH(string): This built-in function tells us how many characters are in the string.
  5. SUBSTR(string, position, length): This function extracts a part of a string. We will use SUBSTR(v_string, i, 1) to get the single character at position i.
  6. FOR Loop with REVERSE: We will use a FOR loop to iterate through the string. The REVERSE keyword makes the loop count backward (e.g., from 10 down to 1), which is perfect for this task.
  7. 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

  1. DECLARE section: We create v_string ('Hello World'), v_reversed_string (which is NULL at the start), and v_length.
  2. BEGIN section: The logic starts.
  3. v_length := LENGTH(v_string);: This calculates the length of 'Hello World' and stores 11 in the v_length variable.
  4. FOR i IN REVERSE 1..v_length LOOP: This is our loop. Because we used REVERSE and v_length is 11, the loop will run with i taking the values: 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, and finally 1.
  5. 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'
  6. END LOOP;: The loop finishes.
  7. DBMS_OUTPUT.PUT_LINE(...): The program prints the original string and the final value of v_reversed_string.
Get more Vinish.dev tutorials on Google

See my articles more often when you search Google.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE, author of four books on Oracle APEX, SQL and PL/SQL, and Oracle Forms, and a software developer building Oracle database applications since 2001.

guest

0 Comments
Oldest
Newest Most Voted