1 Answers
Algorithm to Determine if a String is a Palindrome
In order to determine if a given string is a palindrome, you can follow the below algorithm:
function isPalindrome(str) {
let leftIndex = 0;
let rightIndex = str.length - 1;
while (leftIndex < rightIndex) {
if (str[leftIndex] !== str[rightIndex]) {
return false;
}
leftIndex++;
rightIndex--;
}
return true;
}
let inputString = "Your input string here";
if (isPalindrome(inputString)) {
console.log("The string is a palindrome.");
} else {
console.log("The string is not a palindrome.");
}
This algorithm compares characters from the start and end of the string, moving towards the center to determine if it is a palindrome. It returns true if the string is a palindrome, and false otherwise.
Please login or Register to submit your answer