Main Page
 The gatekeeper of reality is
 quantified imagination.

Stay notified when site changes by adding your email address:

Your Email:

Bookmark and Share
Email Notification
findLargestPalindrome [Go Back]
Purpose
The purpose of this script is to find the largest palindrome present in a sentence. Over the decades of programming I've never needed to solve such a task but I was asked about it so I provide one approach (using javascript) below.

Determine the Largest Palindrome in the Sentence (and answer):
Given the sentence "At noon drive the racecar", find the largest palindrome.
For reference, the value returned should be: "racecar".
A palindrome is a sequence that can be read the same forwards and backwards like "cat", "131", dates like "12/11/21" and some molecular biology 4 and 8 nucleotide sequences (but I do not want to get into fancy pants here).


Function Logic Output:


Full Working Code You Can Copy:
<html>
<head>
<script language="javascript" type="text/javascript">
function findLargestPalindrome(sequence) {
	/* Create array of words from the sentence */
	var sentenceWords = sequence.toLowerCase().split(" ");
	var largestPalindrome = "", largestPalindromeCharacterCount = 0;
	for (var p = 0; p < sentenceWords.length; p++) {
		var word = sentenceWords[p];
		/* Reverse word */
		var wordReverse = "";
		for (var w = (word.length - 1); w >= 0; w--) {
			wordReverse += word.charAt(w);
		}
		/* Save if larger palindrome */
		if (word == wordReverse) {
			if (word.length > largestPalindromeCharacterCount) {
				largestPalindromeCharacterCount = word.length;
				largestPalindrome = word;
			}
		}
	}
	return(largestPalindrome);
}
</script>
</head>
<body onload='javascript:document.getElementById("output").value = findLargestPalindrome("At noon drive the racecar");'>
	Given the sentence "At noon drive the racecar", find the largest palindrome.<br />
	<span style="font-size:9pt">For reference, the value returned should be: "racecar".  A palindrome is a sequence that can be read the same forwards and backwards like "cat", "131", dates like "12/11/21" and some molecular biology 4 and 8 nucleotide sequences (but I do not want to get into fancy pants here).</span>
	<br /><br />
	<strong>Function Logic Output:</strong><br />
	<textarea name="output" id="output" rows="4" cols="30"></textarea>
</body>
</html>
About Joe