---
title: "Implement a function to check if a given string is an anagram of another string."  
description: "Implement a function to check if a given string is an anagram of another string."  
author: "Utpal Vishwas"  
published: 2023-06-14  
updated: 2023-06-15  
canonical: https://www.mindstick.com/forum/158744/implement-a-function-to-check-if-a-given-string-is-an-anagram-of-another-string  
category: "c#"  
tags: ["c#", "string", "programs"]  
reading_time: 2 minutes  

---

# Implement a function to check if a given string is an anagram of another string.

Implement a [function to check](https://www.mindstick.com/forum/161900/write-a-function-to-check-if-a-number-is-prime-with-explanation) if a given [string is an anagram](https://www.mindstick.com/forum/158768/write-a-python-program-to-check-if-a-string-is-an-anagram-of-another-string) of another string.

## Replies

### Reply by Aryan Kumar

Sure, here is a C# [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to [check if](https://www.mindstick.com/forum/12878/how-to-check-if-an-asp-dot-net-file-upload-control-has-a-file-in-jquery) a given [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) is an anagram of another string:

C#

```plaintext
using System;

public class Anagram {

    public static bool IsAnagram(string str1, string str2) {

        // Check if the lengths of the strings are not equal
        if (str1.Length != str2.Length) {
            return false;
        }

        // Create a character array to store the characters of the first string
        char[] ch1 = str1.ToCharArray();

        // Create a character array to store the characters of the second string
        char[] ch2 = str2.ToCharArray();

        // Sort the characters of the first string
        Array.Sort(ch1);

        // Sort the characters of the second string
        Array.Sort(ch2);

        // Compare the sorted characters of the two strings
        for (int i = 0; i < ch1.Length; i++) {
            if (ch1[i] != ch2[i]) {
                return false;
            }
        }

        // Return true if the two strings are anagrams
        return true;
    }
}
```

This function will first check if the lengths of the two strings are not equal. If they are not equal, the function will return false. If the lengths of the two strings are equal, the function will then create a character array to store the characters of each string. The function will then sort the characters of each string. The function will then compare the sorted characters of the two strings. If the sorted characters of the two strings are equal, the function will return true. Otherwise, the function will return false.


---

Original Source: https://www.mindstick.com/forum/158744/implement-a-function-to-check-if-a-given-string-is-an-anagram-of-another-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
