Find Palindrom In PHP

Algoritma

  1. Input String
  2. Max string 2^63
  3. Check String Palindrome or Not Palindrome

Interface

palindromnot palindrom

Source palindrom.php

<html>
<head>
<title>Find Palindrome</title>
</head>
<body>
<h2>Find Palindrome </h2>
<form action=”” method=”post”>
<label>Enter a word or a sentence<label>
<input type=”text” name=”string” />
<input type=”submit” value=”Submit”/>
</form>
</body>
</html>
<?php
if($_POST){

$string = $_POST[‘string’];

//remove all spaces
$string = str_replace(‘ ‘, ”, $string);

//remove special characters
$string = preg_replace(‘/[^A-Za-z0-9\-]/’, ”, $string);

//change case to lower
$string = strtolower($string);

//calculate string length
$strlength = strlen($string);
echo ‘Characters = ‘;
echo $strlength;

//reverse character
$reverse = strrev($string);

//max character = 2 pangkat 63 (9223372036854775808)
if(strlen($string) >= 9223372036854775808){
echo ‘<br><b>Max Characters >= 9223372036854775808)’;

//check palindrome
}elseif($string == $reverse){
echo “<p><b>It is Palindrome</p>”;
}else{
echo “</p><b>Not Palindrome</p>”;
}
}
?>

Iklan

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s