Tuesday, 3 February 2015

C++ program to find the string is anagram

 

Anagrams are strings that are formed by re arranging the charcters from another word.
Eg: Creative and reactive.


#include<iostream>
#include<string>

int main()
{
  std::string s1="creative",s2="reactive";

  if(s1.length() != s2.length())
  {
    std::cout<<"Not a anagram";
    return false;  
  }

  int len=s1.length();
  int i;
  for( i=0; i<len; i++)
  {
    int pos=s2.find(s1[i]);

    {
      s2.erase(pos,1);
    }
    else
    {
      std::cout<<"Not matched!!";
    }
  }

  if(s2.length() == 0)
  {
    std::cout<<"String is anagram";
  }

  return 0;
}