w3resource

PHP Challenges: Check whether a given string is an anagram of another given string

PHP Challenges - 1: Exercise-21 with Solution

Write a PHP program to check whether a given string is an anagram of another given string.

Input : ('anagram','nagaram')

According to Wikipedia an anagram is direct word switch or word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example, the word anagram can be rearranged into nag-a-ram.

Explanation :

PHP: Check whether a given string is an anagram of another given string

Sample Solution :

PHP Code :

<?php
// Function to check if two strings are anagrams
function is_anagram($a, $b)
{
    // Check if the character counts of both strings are equal
    if (count_chars($a, 1) == count_chars($b, 1))
    {
        // If equal, the strings are anagrams
        return "These two strings are anagrams";
    }
    else
    {
        // If not equal, the strings are not anagrams
        return "These two strings are not anagrams";
    }
}
// Test the is_anagram function with two pairs of strings and print the results
print_r(is_anagram('anagram', 'nagaram') . "\n");
print_r(is_anagram('cat', 'rat') . "\n");
?>

Explanation:

Here is a brief explanation of the above PHP code:

  • Function definition:
    • The code defines a function named "is_anagram($a, $b)" which takes two strings '$a' and '$b' as input parameters.
  • Inside the function:
    • It uses the "count_chars()" function to count the occurrences of each character in both strings.
    • It compares character counts of both strings using ==. If they are equal, it means that both strings have the same characters with the same frequency, indicating they are anagrams.
    • If the character counts are not equal, it means the strings are not anagrams.
  • Function Call & Testing:
    • The "is_anagram()" function is called twice with different pairs of strings ('anagram', 'nagaram') and ('cat', 'rat').
    • Finally the "print_r()" function displays whether each pair of strings are anagrams or not.

Sample Output:

This two strings are anagram                                
This two strings are not anagram  

Flowchart:

PHP Flowchart: Check whether a given string is an anagram of another given string

PHP Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a PHP program to get the Hamming numbers upto a given numbers also check whether a given number is a Hamming number.
Next: Write a PHP program to push all zeros to the end of an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/challenges/1/php-challenges-1-exercise-21.php