• Javascript
  • Python
  • Go

How to Check if a String is a Palindrome

A palindrome is a word, phrase, or sequence that reads the same backward as forward. Some common examples of palindromes are "mom", "racecar...

A palindrome is a word, phrase, or sequence that reads the same backward as forward. Some common examples of palindromes are "mom", "racecar", and "level". In this article, we will discuss how to check if a string is a palindrome using different methods in HTML.

Method 1: Using JavaScript

One of the easiest ways to check if a string is a palindrome is by using JavaScript. To do this, we first need to create a function that takes in a string as a parameter. Inside the function, we will convert the string to lowercase and remove all non-alphanumeric characters using the replace() method. Then, we will use the split() method to convert the string into an array and reverse it using the reverse() method. Finally, we will join the reversed array back to a string and compare it with the original string. If they are equal, then the string is a palindrome.

Here's the code for the function:

<script>

function checkPalindrome(str) {

var lowerCaseStr = str.toLowerCase();

var alphanumericStr = lowerCaseStr.replace(/[^a-z0-9]/g, '');

var reversedStr = alphanumericStr.split('').reverse().join('');

if (reversedStr === alphanumericStr) {

return true;

} else {

return false;

}

}

</script>

To use this function in HTML, we can create a form where the user can input a string and then display the result using JavaScript. Here's an example:

<form>

<input type="text" id="inputString">

<button onclick="displayResult()">Check</button>

</form>

<p id="result"></p>

<script>

function displayResult() {

var input = document.getElementById("inputString").value;

var result = document.getElementById("result");

if (checkPalindrome(input)) {

result.innerHTML = input + " is a palindrome.";

} else {

result.innerHTML = input + " is not a palindrome.";

}

}

</script>

Method 2: Using CSS

Another way to check if a string is a palindrome is by using CSS. This method involves creating a div with the string as its content and then using the :before and :after pseudo-elements to reverse the text and compare it with the original text. If they are the same, then the string is a palindrome.

Here's an example of the CSS code:

<style>

.palindrome:before {

content: attr(data-content);

display: none;

}

.palindrome:after {

content: '';

display: block;

white-space: pre;

}

.palindrome:before,

.palindrome:after {

font-size: 0;

line-height: 0;

}

</style>

<div class="palindrome" data-content="racecar"></div>

In this example, the div with the class "palindrome" has the string "racecar" as its data-content. The :before pseudo-element will display the reversed text "racecar" and the :after pseudo-element will display an empty string. Since they are the same, the string is a palindrome. If the string is not a palindrome, then the reversed text will not match the original text.

Method 3: Using PHP

If you are using a server-side language like PHP, you can also check if a string is a palindrome. The logic is similar to the JavaScript method, but instead of using JavaScript, we will use PHP functions. Here's an example of the PHP code:

<?php

function checkPalindrome($str) {

$lowerCaseStr = strtolower($str);

$alphanumericStr = preg_replace("/[^a-z0-9]/", "", $lowerCaseStr);

$reversedStr = strrev($alphanumericStr);

if ($reversedStr === $alphanumericStr) {

return true;

} else {

return false;

}

}

?>

To use this function, we can create a form similar to the one in the JavaScript method and then display the result using PHP. Here's an example:

<form method="post">

<input type="text" name="inputString">

<input type="submit" name="submit" value="Check">

</form>

<?php

if (isset($_POST['submit'])) {

$input = $_POST['inputString'];

if (checkPalindrome($input)) {

echo $input . " is a palindrome.";

} else {

echo $input . " is not a palindrome.";

}

}

?>

In conclusion, there are various ways to check if a string is a palindrome using HTML. You can use JavaScript, CSS, or a server-side language like PHP to achieve the same result. Choose the method that works best for you and have fun experimenting!

Related Articles

Signal Peak Detection

Signal Peak Detection: A Vital Tool in Electronic Communication In today's world, we are constantly bombarded with information from various ...

Which rule engine is best for me?

When it comes to decision-making processes in computer programming, rule engines are a crucial tool that can help automate and streamline wo...