Are 'Listen' and 'Silent' anagrams? Yes
Is 'A man, a plan, a canal, Panama!' a palindrome? Yes
Original: aaabbbccdaaa
Compressed: a3b3c2d1a3
<?php
include('includes/header.php');
// Function to check if two strings are anagrams
function are_anagrams($str1, $str2)
{
// Remove spaces and convert to lowercase
$str1 = preg_replace('/\s+/', '', strtolower($str1));
$str2 = preg_replace('/\s+/', '', strtolower($str2));
// Sort the characters and compare
return count_chars($str1, 1) == count_chars($str2, 1);
}
// Function to check if a string is a palindrome
function is_palindrome($input)
{
// Normalize the string
$normalized = preg_replace("/[^A-Za-z0-9]/", '', strtolower($input));
return $normalized === strrev($normalized);
}
// Function to compress a string
function compress_string($input)
{
$compressed = '';
$count = 1;
for ($i = 0; $i < strlen($input); $i++) {
if ($i < strlen($input) - 1 && $input[$i] === $input[$i + 1]) {
$count++;
} else {
$compressed .= $input[$i] . $count;
$count = 1; // reset count
}
}
return $compressed;
}
// Example usages
echo "<h1>Binita's String Functions</h1>";
$anagram_check = are_anagrams("Listen", "Silent");
echo "<p>Are 'Listen' and 'Silent' anagrams? " . ($anagram_check ? 'Yes' : 'No') . "</p>";
$palindrome_check = is_palindrome("A man, a plan, a canal, Panama!");
echo "<p>Is 'A man, a plan, a canal, Panama!' a palindrome? " . ($palindrome_check ? 'Yes' : 'No') . "</p>";
$input_string = "aaabbbccdaaa";
$compressed_string = compress_string($input_string);
echo "<p>Original: $input_string</p>";
echo "<p>Compressed: $compressed_string</p>";
include('includes/footer.php');
?>