Write a function named isPalindrome that determines whether a given string is a palindrome.

Learner Level
Loop
A palindrome is a word, phrase, or sequence that reads the same backward as forward, ignoring case and non-alphanumeric characters.
Example 1
Input:
s (string) = madam
Return:
(boolean) true
Example 2
Input:
s (string) = hello
Return:
(boolean) false
Example 3
Input:
s (string) = A man, a plan, a canal, Panama
Return:
(boolean) true
Example 4
Input:
s (string) = No lemon, no melon
Return:
(boolean) true

A string is considered a palindrome if it reads the same forward and backward, ignoring spaces, punctuation, and capitalization. To check if a string is a palindrome, first sanitize it by removing unwanted characters and converting it to the same case. Then, compare the sanitized string with its reverse.

To check if a string is a palindrome without using built-in reverse functions, follow these steps:

  1. Check for Empty or Null Input
    If the string is empty or null, return true directly or handle it as a special case (empty strings are generally considered palindromes).

  2. Sanitize the String
    Ignore spaces, punctuation, and capitalization:

    • Remove all non-alphanumeric characters.

    • Convert all characters to lowercase.

  3. Compare Characters from Both Ends
    Use a loop with two pointers:

    • One starting from the beginning.

    • One from the end.
      Compare corresponding characters while moving inward.

  4. Return Result
    If all characters match, return true; otherwise, return false.

Pseudocode:

Function isPalindrome(s):
    Initialize an empty string cleanString
    For each character ch in s:
        If ch is alphanumeric:
            Append lowercase of ch to cleanString

    Set reversedString = reverse of cleanString

    If cleanString is equal to reversedString:
        Return true
    Else:
        Return false
Run your code to see the result.