Thursday, September 29, 2022

C++ Code to check two given String are Anagram are not

 

What is Anagram : An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once


C++ Code to check two given String are Anagram are not


#include <iostream>

#include <string.h>

using namespace std;

void checkAnagrams(char str1[],char str2[])

{

     int hash[26];

     int i;

     for( i=0;i<26;i++)

     hash[i]=0;

     

     for(i=0;i<strlen(str1);i++)

     {

         hash[str1[i]-97]++;

     }

      for(i=0;i<strlen(str2);i++)

      {

         hash[str2[i]-97]--;

      }

                    

      for(i=0;i<26;i++)

      {

            if(hash[i]!=0)

            break;

      }

      if(i==26)

      cout<<"Strings are anagrams";

      else

      cout<<"Strings  are not anagrams";

}

                               

int main(int argc, char *argv[])

{

    checkAnagrams("madam curie","radium came");

  

    return EXIT_SUCCESS;

}



Output :

Strings are anagrams

No comments:

Post a Comment