You are just a step away from learning anagrams of  string Using C++

You are just a step away from learning anagrams of string Using C++

An anagram of a string is a string that contains the same characters as of another string, only the order of characters can be changed.

For example, “nectarpost” and “postnectar”

Steps to writing programming for anagrams of string

Step 1: Iterate 1st and 2nd String.

Step 2:  Maintain a count of the alphabets of both the strings in respective arrays.

Step 3: If both iterated strings result in matching array, then we say that the given string is anagrams.

int let_me_check_anagram(char a[], char b[])
{
   int cEnglish_alphabets_first[26] = {0},     cEnglish_alphabets_second[26] = {0}, c=0;
   while (a[c] != '\0')
  {
      cout<< "First : " <<a[c]<< ": "<<(int)a[c] <<"\n";
      cEnglish_alphabets_first[a[c]-'a']++;
      c++;
   }

   int i=0;
   while (b[i] != '\0')
   {
      cout<< "Second : " <<b[i]<< ": "<<(int)b[i] <<"\n";
      cEnglish_alphabets_second[b[i]-'a']++;
      i++;
   }
// Comparing frequency of characters

  for (c = 0; c < 26; c++)
  {
      cout<< "First Compate : "<< cEnglish_alphabets_first[c] << endl;
      cout<< "Second Compate : "<< cEnglish_alphabets_second[c] << endl;
      if (cEnglish_alphabets_first[c] != cEnglish_alphabets_second[c])
      return 0;
   }

    return 1;
}

int main()
{
char a[100], b[100];

cout<<“Enter any two strings to find string is anagrams: \n”; cin>>a;
cin>>b;

if (let_me_check_anagram(a, b) == 1)
printf(“Yepee!!! strings are anagrams\n”);
else
printf(“hmmm!!! The strings are not anagrams\n”);

return 0;
}

Read More…

Leave a Reply