C++ Program to check strings are anagram or not


In this program, You will learn how to check strings are anagram or not in C++.


Enter First String :triangle

Enter Second String :integral

If Both string characters are same in any order it's called anagram


Example: How to check strings are anagram or not in C++

Run it Now
#include<iostream>
#include<cstring>
using namespace std;

int main() {

    char str1[100];
    char str2[200];
    int len1, len2, size = 0;
    int j, i;

    cout << "Enter First String :";
    cin.getline(str1, 100);

    cout << "\nEnter Second String :";
    cin.getline(str2, 100);

    len1 = strlen(str1);
    len2 = strlen(str2);

    if (len1 == len2) {
        for (i = 0; i < len1; i++) {
            j = 0;
            while (str2[j] != '\0') {
                if (str1[i] == str2[j]) {
                    size++;
                    break;
                }
                j++;
            }
        }
    }

    if (len1 == size) {
        cout << "\nStrings are Anagram";
    } else {
        cout << "\nStrings are Not Anagram";
    }

    return 0;
}

Output:

Enter First String :triangle
Enter Second String :integral
Strings are Anagram