I am trying to write a program with a function that takes two char arrays, array1, array2. The function should compare array2 with array1, if array2 is an anagram of array1, then it should indicate so. Here is my idea:
I am writing a bool function and I have two loops in the function. First, just pick one character from array1, loop it through array2, and whenever there is an encounter, increase count by 1.
Problem is, this will count through every character, including repeatedly counting every character that appears again. I need help in counting the number of occurrences of each character only once, and ignoring any repeated one. That way, I can easily compare the two strings, and return false if the count is not equal to the length of string1. And here is the function:
Code:bool IsAnagram(char *array1, char *array2) { bool anagram = true; int counter = 0; for (int i = 0; array1[i]; ++i) { for (int k = 0; array2[k]; ++k) if(array1[i] == array2[k]) counter++; //if counter is not equal to length of array1, return anagram = false; } }



LinkBack URL
About LinkBacks



