Original: Hello, World!
Reversed: !dlroW ,olleH
String: 'This is an example string.' has 7 vowels.
Word count: 8
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 reverse a string
function reverse_string($input)
{
return strrev($input);
}
// Function to count the number of vowels in a string
function count_vowels($input)
{
$vowels = "aeiouAEIOU";
$count = 0;
for ($i = 0; $i < strlen($input); $i++) {
if (strpos($vowels, $input[$i]) !== false) {
$count++;
}
}
return $count;
}
// Function to count the number of words in a string
function word_count($input)
{
return str_word_count($input);
}
// Function to check if two strings are anagrams
function are_anagrams($str1, $str2)
{
// Normalize the strings
$str1 = preg_replace('/\s+/', '', strtolower($str1));
$str2 = preg_replace('/\s+/', '', strtolower($str2));
// Sort and compare
return count_chars($str1, 1) == count_chars($str2, 1);
}
// Function to check if a string is a palindrome
function is_palindrome($input)
{
$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;
}
// Main code demonstrating function usage
echo "<h1>Binita String and Number Functions</h1>";
// Reverse a string
$original_string = "Hello, World!";
$reversed_string = reverse_string($original_string);
echo "<p>Original: $original_string</p>";
echo "<p>Reversed: $reversed_string</p>";
// Count vowels
$vowel_string = "This is an example string.";
$vowel_count = count_vowels($vowel_string);
echo "<p>String: '$vowel_string' has $vowel_count vowels.</p>";
// Count words
$word_string = "Counting the number of words in this sentence.";
$word_count_result = word_count($word_string);
echo "<p>Word count: $word_count_result</p>";
// Anagram check
$anagram1 = "Listen";
$anagram2 = "Silent";
$anagram_check = are_anagrams($anagram1, $anagram2);
echo "<p>Are '$anagram1' and '$anagram2' anagrams? " . ($anagram_check ? 'Yes' : 'No') . "</p>";
// Palindrome check
$palindrome_string = "A man, a plan, a canal, Panama!";
$palindrome_check = is_palindrome($palindrome_string);
echo "<p>Is '$palindrome_string' a palindrome? " . ($palindrome_check ? 'Yes' : 'No') . "</p>";
// Compress a string
$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');
?>