-4

I am trying to create a programme that is able to determine whether two inputted words are anagrams of each other.

The way in which I have been told to go by my tutor is to count how many of the first letter of one word there is, then compare to the other, then repeat for the rest of the letters. Therefore if the word gets to the end, then it considers them anagrams. However that is all he has helped me with, and I am really struggling with the problem.

The programme is required to print whether or not they are anagrams like so,

Success! "Carthorse" and "Orchestra" are anagrams!

Edit: Thanks guys for all of your responses, whilst I understand the whole idea behind them, I am finding it very difficult to put them into code, would anyone be able to simply writing the annotated code for me? It's not for a homework or anything, it's simply a personal project.

  • http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list for a short start you will need an array – Mahmut Ali ÖZKURAN Mar 03 '15 at 23:08
  • Another way would be to allocate a map (or rather an array) of each character to its count. Increment it for the first word, decrement for the second, if it ends up at all zeroes you have an anagram. The advantage is the predictable execution time, but you spend some memory. – tux3 Mar 03 '15 at 23:09
  • @tux3 Essentially like my answer and perhaps more elegant. Nice. – Alan Mar 03 '15 at 23:11

3 Answers3

1

It sounds like you're new to C! Welcome :)

Tasks like that can seem complex, so the first step I'd do here is break it down into steps that you can google for how to do. So:

"count how many of the first letter of one word there is, then compare to the other, then repeat for the rest of the letter"

  1. Read in the words/create variables of them
  2. Create an array of length 26, to store each letter of the alphabet
  3. Loop through the first word and for each letter, add one to the correct array index (a = 0, m = 12, etc) e.g.

    int index = string[i] - 'a'; // This will subtract the ascii value from the letter, getting a = 0 etc letterCounts[index]++; // or letterCounts[index]--;

  4. Loop through the second word, and for each letter, subtract one from the array index

  5. If at the end any index is not 0, it is not an anagram.
Beth Crane
  • 625
  • 3
  • 8
  • Thanks for the quick reply, and yes I am very new. I understand steps 1,2 and 5 but 3 and 4, I'm not sure how to implement - anyone willing to elaborate? – Danthescotman Mar 03 '15 at 23:23
  • You'll want something like this http://stackoverflow.com/questions/3213827/how-to-iterate-over-a-string-in-c to loop through the strings. I've edited the above for the arrays – Beth Crane Mar 03 '15 at 23:37
0

Convert both strings to lowercase characters.

Create two arrays of 26 characters for the letters of the alphabet.

Run through each string counting the letters and incrementing the appropriate element in the alphabet arrays.

Then compare the two alphabet arrays and if they are equal for each character, your strings are anagrams.

Alan
  • 716
  • 5
  • 15
  • Thanks for the quick reply! Yes that all makes sense but my problem is that I am really struggling with putting that all into actual code and implementing it – Danthescotman Mar 03 '15 at 23:28
  • @Dan BethCrane's answer above is the right track to work with. It's a more detailed and more elegant version of my answer (uses less storage space). – Alan Mar 03 '15 at 23:43
0

1) Convert both strings to lowercase as necessary (use tolower from ctype.h).

2) Sort each string, e.g., by using qsort from stdlib.h:

static int cmp(const void *a, const void *b) { return *(char *)a - *(char *)b; }

qsort(str1, strlen(str1), 1, (cmp));
qsort(str2, strlen(str2), 1, (cmp));

3) Compare the sorted strings with strcmp from string.h - if equal, they are anagrams, otherwise not.

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • This is probably the solution with the least amount of code you need to write in C. Theoretically the other suggested solution of counting occurrences of letters is faster, but that makes no practical difference when the strings are less than hundreds of thousands characters long. If this is homework, try both solutions so you learn both how to do it yourself and how to use library functions (also try to implement a simple sorting algorithm, such as selection sort or insertion sort, and replace the library `qsort` with it). – Arkku Mar 03 '15 at 23:51