An anagram is a word formed from another by rearranging its letters, using all t
ID: 663446 • Letter: A
Question
An anagram is a word formed from another by rearranging its letters, using all the original letters exactly once; for example, orchestra can be rearranged into carthorse.
Write a function that checks if two words are each other's anagrams.
For example, AreStringsAnagrams("momdad", "dadmom") should return true as arguments are anagrams.
Edit this code:
using System;
public class AreAnagrams
{
public static bool AreStringsAnagrams(string a, string b)
{
throw new NotImplementedException("Waiting to be implemented.");
}
public static void Main(string[] args)
{
Console.WriteLine(AreStringsAnagrams("momdad", "dadmom"));
}
}
Explanation / Answer
#include int check_anagram(char [], char []); int main() { char a[100], b[100]; int flag; printf("Enter first string "); gets(a); printf("Enter second string "); gets(b); flag = check_anagram(a, b); if (flag == 1) printf(""%s" and "%s" are anagrams. ", a, b); else printf(""%s" and "%s" are not anagrams. ", a, b); return 0; } int check_anagram(char a[], char b[]) { int first[26] = {0}, second[26] = {0}, c = 0; while (a[c] != '') { first[a[c]-'a']++; c++; } c = 0; while (b[c] != '') { second[b[c]-'a']++; c++; } for (c = 0; c < 26; c++) { if (first[c] != second[c]) return 0; } return 1; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.