A string is generally called a palindrome string when the reverse of the string is the same as the original string. For example dad, level, and noon.
Example 1: Input: Str = “LEVEL” Output: True // Which means the string is palindrome Explanation: String when reversed is the same as string. Example 2: Input: Str = “AJAY” Output: False // Which means that the string is not palindrome Explanation: String when reversed is not the same as string.
In the above example, we have created isPal() function and in it, we are trying to first split the string and then reverse it. After reversing the string we are checking whether the reverse string is the same as the original string or not. If both are the same then we are console logging true on the screen otherwise we are just displaying false on the screen.
In the above example, we have initialized the new string with the value of the old string and just converted the old string to lowercase. Other than that we have also taken two variables which are left and right. The left variable has been initialized with zero and the right variable is initialized with the value (length of string – 1). This is done to compare the first character of the string with the last character for the first iteration. If the first character is not equal to the last character then we are just returning false and incrementing the left value by 1 and decrementing the right by 1. Otherwise, we are just returning true if they both are the same as it generally denotes that the string is Palindrome.