The string is palindrom if reverted it equals to source string. Here is simple algorithm to detect is the source string a palindrome or not.
CREATE OR REPLACE PROCEDURE isPalindrom(l_string VARCHAR2) IS src_string VARCHAR2(32767) := l_string; len_src_string INTEGER := LENGTH(src_string); j INTEGER; FUNCTION getChar(i INTEGER) RETURN CHAR IS BEGIN RETURN SUBSTR(src_string, i, 1); END; BEGIN j := 1; LOOP EXIT WHEN (j = ROUND(len_src_string/2) + 1) OR getChar(j) <> getChar(len_src_string-j+1); j := j+1; END LOOP; IF getChar(j) = getChar(len_src_string-j+1) THEN DBMS_OUTPUT.PUT_LINE(l_string||' IS POLYNDROM'); ELSE DBMS_OUTPUT.PUT_LINE(l_string||' IS NOT POLYNDROM'); END IF; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('SQLCODE: ' || SQLCODE); DBMS_OUTPUT.PUT_LINE('SQLERRM: ' || SQLERRM); END;
No comments:
Post a Comment