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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a C# function to check if a given string is an anagram of another string:
C#
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.