C++ Program to Check whether two Strings are Anagrams

This is a C++ Program to Find if Two Strings are Anagrams.

Problem Description

The program takes two strings and checks if they are anagrams. An anagram is a phrase formed by rearranging the letters of a different phrase.

Problem Solution

1. The program takes two strings.
2. Using a function, the two strings are sorted and checked if they are anagrams.
3. The result is printed.
4. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find if Two Strings are Anagrams. The program output is shown below.

  1. #include <iostream>
  2. using namespace std;
  3. int anagram(char str1[], char str2[])
  4. {
  5.     int i, flag = 0,  x[26] = {0}, y[26] = {0};
  6.     for(i = 0; str1[i] != '\0'; i++)
  7.         x[str1[i] - 'a']++;
  8.     for(i = 0; str2[i] != '\0'; i++)
  9.         y[str2[i] - 'a']++;
  10.     for (i = 0; i < 26; i++)
  11.     {
  12.         if (x[i] != y[i])
  13.             flag = 1; 
  14.     }
  15.     if (flag == 1)
  16.         cout << "Entered strings are not anagrams.";
  17.     else
  18.         cout << "Entered strings are anagrams.";
  19. }
  20.  
  21. int main ()
  22. {
  23.     char str1[50], str2[50];
  24.     int flag;
  25.     cout << "Enter string 1 : ";
  26.     gets(str1);
  27.     cout << "Enter string 2 : ";
  28.     gets(str2);
  29.     anagram(str1, str2);
  30.     return 0;
  31. }
Program Explanation

1. The user is asked to enter two strings and stored in ‘str1’ and ‘str2’.
2. The function ‘anagram()’ is called, passing the two strings.
3. Within the function, the frequency of each character is calculated using a for loop for both the strings.
4. Again using a for loop, frequency of both the strings is compared.
5. If they are not equal, the strings are not anagrams, else they are.
6. The result is then printed.

advertisement
advertisement
Runtime Test Cases
Case 1 :
Enter string 1 : creative
Enter string 2 : reactive
Entered strings are anagrams.
 
Case 2 :
Enter string 1 : mom10
Enter string 2 : 10dad
Entered strings are not anagrams.
 
Case 3 :
Enter string 1 : 24 lessons
Enter string 2 : son24 less
Entered strings are anagrams.

Sanfoundry Global Education & Learning Series – C++ Programs.

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.