---
title: "Write a PL/SQL code to find whether a given string is a palindrome or not."  
description: "Write a PL/SQL code to find whether a given string is a palindrome or not."  
author: "Revati S Misra"  
published: 2023-04-04  
updated: 2023-04-05  
canonical: https://www.mindstick.com/forum/157684/write-a-pl-sql-code-to-find-whether-a-given-string-is-a-palindrome-or-not  
category: "database"  
tags: ["database", "mysql", "sql server", "plsql"]  
reading_time: 2 minutes  

---

# Write a PL/SQL code to find whether a given string is a palindrome or not.

Write a PL/[SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) to find whether a given [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) is a palindrome or not.

## Replies

### Reply by Krishnapriya Rajeev

The following PL/SQL program checks whether the given string is a palindrome or not:

```plaintext
SET SERVEROUTPUT ON
DECLARE
     -- declare the string to be checked
     str VARCHAR2(10) := 'RACECAR';
     rev VARCHAR(10);
     c VARCHAR2(1);

begin
     -- for each character from last to first
     FOR i IN REVERSE 1..Length(str) LOOP
           -- extract each character from string
           c := Substr(str, i, 1);
         
           -- concatenate each character to reverse string
           rev := rev || c;
     END LOOP;
    
     -- check if both string and its reverse are equal
     IF rev = str
     THEN
           dbms_output.put_line(str || ' is a palindrome.');
     ELSE
           dbms_output.put_line(str || ' is not a palindrome.');
     END IF;
END;
/
-- OUTPUT : RACECAR is a palindrome.
```

We first declare the variables *str* (the string to be checked), *rev* (to store the reversed string), and *c* (to extract each character). Using a FOR loop that iterates from the length of the string to 1, we extract each character from the string and concatenate it to the reversed string. Finally, we check if *str* and *rev* are equal. If they are, the string is a palindrome.


---

Original Source: https://www.mindstick.com/forum/157684/write-a-pl-sql-code-to-find-whether-a-given-string-is-a-palindrome-or-not

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
