Advertisement

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 the First String: triangle

Enter Second String: integral

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

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

#include<iostream>
using namespace std;

#include
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 << "Enter 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 << "Strings are anagram";
    } else {
        cout << "Strings are not anagram";
    }

    return 0;
}

Output:

Enter first string:Xiith.com
Enter second string:iithX.moc
Strings are anagram