Anagrams:

What exactly are these anagrams? Anagram is word or phrase formed by rearranging the words (essentially letters) of different words or phrases usually using all the letters exactly once.

For instance, anagram of ROPE can be PORE; anagram of LEVER would be REVEL; anagram of ‘Strong Woman’ could be ‘Storm Wagon’.

 

Points to remember:

  • The original phrase or word is called as subject.
  • The phrase or word formed rearranging the letters of subject is called as anagram of the subject.
  • Anagrams do not necessarily use all the letters, however they usually do.

Applications:

  • They were used by many scientists and discoveries to reveal and declare their inventions and discoveries. For instance: When Robert Hooke discovered Hooke’s law, he first published it in anagram form, ceiiinosssttuv, for ut tensio, sic vis(Latin: as the tension, so the force) .
  • Anagrams and pseudonyms work hand in hand. They usually act as a shield to conceal identitiy. For e.g: H.A. LARGELAMB, an anagram of A. GRAHAM BELL  (Alexander Graham Bell).
  • Anagrams are also used as a recreational activity and in games. The very popular magazine/ news paper puzzle: Jumble / Scramble.
  • Can be used for generating passwords as it is a task to remember different passwords across different accounts. Using anagrams would ease this task.
  • It is also used as a naive approach in ciphering as well. It can be used as a simple encryption technique to send across secret messages.
  • Their origin dates back to history when they were considered as revealing mystic or prophetic messages.

Approach:

  1. After accepting input strings – str1 and str2 we call the function check_anagram in order to check whether the provided strings are anagram of each other or not.
  2. The check_anagram function initializes two arrays of size 26 elements – count1 and count2 , for counting the occurrence of characters a-z in strings.
  3. The logic is, we count occurrences of each alphabet in respective strings and next compare to check if the occurrences of each alphabet in both the strings is equal or not.
  4. If it is equal , they are anagrams else they are not anagrams.

Code:

#include <stdio.h>

int check_anagram(char str1[], char str2[])
{
int count1[26] = {0}, count2[26] = {0}, i = 0;

while (str1[i] != '\0')         //counter for all alphabets in string 1
{
count1[str1[i] - 'a']++;
i++;
}

i = 0;

while (str2[i] != '\0')              //counter for all alphabets in string 2
{
count2[str2[i] -'a']++;
i++;
}

for (i = 0; i < 26; i++)
{
if (count1[i] != count2[i])
return 0;
}

return 1;
}

int main()
{
char *str1, *str2;
int size = 100;

printf("Enter the first string\n");
str1 = (char*)malloc(size);
getline(&str1, &size, stdin);

printf("Enter the second string\n");
str2 = (char*)malloc(size);
getline(&str2, &size, stdin);

if (check_anagram(str1, str2) == 1)
printf("The strings are anagrams of each other.\n");
else
printf("The strings are not anagrams of each other.\n");

return 0;
}

Output:

 

Case 1: Strings are anagrams of each other:

Enter the first string
creation

Enter the second string
reaction

The strings are anagrams of each other.

Case 2: Strings are not anagrams of each other.

Enter the first string
rope

Enter the second string
pope

The strings are not anagrams of each other.

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019