Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a boolean function that determines if a string argument is a palindrome. T

ID: 646793 • Letter: W

Question

Write a boolean function that determines if a string argument is a palindrome. The

function should return true if the argument reads the same forwards and backwards.

Your function should ignore spaces and be case-insensitive. Assume

the input is just letters and spaces. Hint: you can do recursive calls on any substring

of the original one (as long as it is shorter, and you have enough base cases).

I have wrote most of the code, I just cannot figure out how to get the function to ignore spaces. Any help is greatly appreciated.

bool isPalindrome(string word) {

int strlen = word.size();

if (word[0] != word[strlen - 1])

return false;

else if (strlen <= 1)

return true;

else

return isPalindrome(word.substr(1, strlen - 2));

}

Explanation / Answer

Answer:

CREATE or replace FUNCTION is_palindrome
(instr IN VARCHAR) RETURN INTEGER IS

strlen INTEGER;
i INTEGER;
strtest VARCHAR2(100);
revstr VARCHAR2(100);
chrtest CHAR(1);
BEGIN
strlen := LENGTH(instr);

FOR i IN 1..strlen LOOP
-- Set upper to make not case sensitive and
-- Ignore punctuation and spaces
chrtest := SUBSTR(UPPER(instr),i,1);
IF chrtest BETWEEN 'A' AND 'Z' OR
chrtest BETWEEN '0' AND '9' THEN
strtest := strtest || chrtest;
END IF;
END LOOP;

-- Reverse the string
FOR i IN REVERSE 1..strlen LOOP
revstr := revstr||SUBSTR(strtest,i,1);
END LOOP;

-- Same backwards and forwards then palindrome
IF strtest = revstr THEN
RETURN(-1);
ELSE
RETURN(0);
END IF;

END;

SELECT is_palindrome('A man, a plan, a canal, Panama!') isit
FROM dual;

ISIT
----------
-1

SELECT is_palindrome('A man, a plan, a ditch, Panama!') isit
FROM dual;

ISIT
----------
0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote