C++ Friend function | Practice | GeeksforGeeks

Ankush challenges Ankit to complete his code which implements the functionality of checking if the first and last name of a student is an anagram of each other or not.
An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “act” and “tac” are anagram of each other. Invest your time to study the locked code and complete it accordingly.
Input:
The first line contains the number of test cases, each test case consists of one line input containing 2 space separated strings representing the first and last name of the student.
Output:
For each test case T, the output should consist of a single line - ANAGRAM or NOT ANAGRAM depending upon if the 2 given strings are anagram of each other or not.
Constraints:
1 <= T <= 100
Example:
Input:
2
rahul garg 
ankit kitan
Output:
NOT ANAGRAM
ANAGRAM
0
amarrajsmart1976 days ago
 map<int,int> mp;   string f=k.first_name;   string l=k.last_name;   if(f.length()!=l.length())   {       cout<<"NOT ANAGRAM"<<endl;       return;   }   for(int i=0;i<f.length();i++)   {       mp[f[i]];   }   for(int i=0;i<l.length();i++)   {       if(mp.find(l[i])==mp.end())       {           cout<<"NOT ANAGRAM"<<endl;           return;       }   }   cout<<"ANAGRAM"<<endl;   return;
0
chandanshoun1 month ago
void check_name(student k){   string f = k.first_name;   string l = k.last_name;      set<char> s1;   set<char> s2;   if(f.size()!=l.size())   {       cout<<"NOT ANAGRAM"<<endl;       return;   }      for(int i=0;i<f.size();i++)   {       s1.insert(f[i]);       s2.insert(l[i]);   }      if(s1==s2)   {       cout<<"ANAGRAM"<<endl;   }   else   {       cout<<"NOT ANAGRAM"<<endl;   }   //Add your code here.}
0
indresh05643 months ago
void check_name(student k){   //Add your code here.   sort(k.first_name.begin(),k.first_name.end());   sort(k.last_name.begin(),k.last_name.end());   if(k.first_name==k.last_name)   cout<<"ANAGRAM\n";   else   {       cout<<"NOT ANAGRAM\n";   }   }
+1
kashyapjhon3 months ago
C++ Solution :
void check_name(student k){   //Add your code here.   sort(k.first_name.begin(),k.first_name.end());   sort(k.last_name.begin(),k.last_name.end());   if(k.first_name==k.last_name){       cout<<"ANAGRAM\n";   }   else{       cout<<"NOT ANAGRAM\n";   }}
0
tushargarg98683 months ago
   //Add your code here   sort(k.first_name.begin(),k.first_name.end());   sort(k.last_name.begin(),k.last_name.end());   if(k.first_name==k.last_name){       cout<<"ANAGRAM"<<endl;   }   else{       cout<<"NOT ANAGRAM"<<endl;   }}
0
Tanmay Malhotra
This comment was deleted.
0
ANKIT SINHA8 months ago
ANKIT SINHA
https://uploads.disquscdn.c...
0
Abhinav Verma11 months ago
Abhinav Verma
void check_name(student k){    //Add your code here.    string s1,s2;    s1= k.first_name;    s2= k.last_name;    int len1,len2,count=0;    len1= k.first_name.length();    len2= k.last_name.length();
    if(len1==len2)    {        sort(s1.begin(),s1.end());        sort(s2.begin(),s2.end());
        for(int i=0;i<len1;i++) {="" if(s1[i]="=s2[i])" {="" count++;="" }="" }="" if(count="=len1)" {="" cout<<"anagram"<<endl;="" }="" else="" {="" cout<<"not="" anagram"<<endl;="" }="" }="" else{="" cout<<"not="" anagram"<<endl;="" }="" }="">
0
Annanya Mathur1 year ago
Annanya Mathur
void check_name(student k){   //cout<<s.first_name.size()<<" "<<s.last_name.size()<<endl;="" sort(k.first_name.begin(),k.first_name.end());="" sort(k.last_name.begin(),k.last_name.end());="" int="" c="0;" cout<<s.first_name.size()<<"="" "<<s.last_name.size()<<endl;="" if(k.first_name.size()!="k.last_name.size())" cout<<"not="" anagram"<<endl;="" else="" {="" for(int="" i="0;i&lt;k.first_name.size();i++)" {="" if(k.first_name[i]!="k.last_name[i])" {="" c="1;" break;="" }="" }="" if(c="=1)" cout<<"not="" anagram"<<endl;="" else="" cout<<"anagram"<<endl;="" }="" }="">
0
Amit kumar Gupta2 years ago
Amit kumar Gupta
Use my simplest code in c++...HAPPY CODING !!
void check_name(student k){   sort((k.first_name).begin(),(k.first_name).end());   sort((k.last_name).begin(),(k.last_name).end());   if(k.first_name==k.last_name)   cout<<"ANAGRAM"<<endl; else="" cout<<"not="" anagram"<<endl;="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.