Pl/SQL is a block-structured language that combines SQL's functionality with procedural commands. In this article, we will discuss a program in PL/SQL to reverse a given string for example −
Input : taerGsIdoG Output : GodIsGreat Explanation : reverse string of “taerGsIdoG” is “GodIsGreat”. Input : LQS Output : SQL Explanation Reverse string of “LQS” is “SQL”.
Approach to find The Solution
- First, you need to find the length of the given string.
- Now you can traverse the line but in a reversed order.
- Store each character in another string while traversing.
- At last, you can print the reversed string.
Example
DECLARE -- declaring variables to be used. input_string VARCHAR(50) := 'taerGsIdoG'; length NUMBER; reversed_string VARCHAR(20); BEGIN -- finding the length of the string. length := Length(input_string); -- traversing the string in reversed order. FOR i IN REVERSE 1.. length LOOP -- storing each character in reversed_string variable reversed_string := reversed_string || Substr(input_string, i, 1); END LOOP; dbms_output.Put_line(‘Reversed string : ' || reversed_string); END;
Output
Reversed string: GodIsGreat
Conclusion
In this article, we discuss a PL/SQL programming language that is very simple; it feels like using ordinary English to give the command to the system, which is a block-structured language. We also discussed a program to reverse a string in PL/SQL language. We hope you find this article helpful.