C++ anagram – C++ Program to Check Strings are Anagram or Not

C++ anagram: In the previous article, we have discussed C++ program to Find Factorial of a Number Using Recursion. In this article, we will see C++ Program to Check Strings are Anagram or Not.

C++ Program to Check Strings are Anagram or Not

  • Write a C++ program to check whether two strings are anagram or not.

In this C++ Program. we will check whether two strings are anagram or not and print message accordingly on screen.

Two strings are said to be anagram, if we can rearrange characters of one string to form another string. In other words, two anagram strings contains same set of characters.
For Example:

  • “debit card” and “bad credit” are anagram strings.
  • “techcrashcourse” and “crashtechcourse” are anagram strings.

Algorithm to Check Anagram Strings

  • The Length of both string must be same, otherwise they cannot be anagram.
  • Count character frequency of first string.
  • Count character frequency of second string.
  • Compare character frequencies of both string. If same, then both strings are anagram otherwise not an anagram.

C++ Program to Check Strings are Anagram or Not

C++ Program to Check Strings are Anagram or Not

//C++ Program to check if two strings are anagram
#include <iostream>
#include <cstring>
using namespace std;
 
int isAnagram(char *first, char *second);
 
int main(){
    char first[100], second[100];
    cout << "Enter first String\n";
    cin.getline(first, 100);
     
    cout << "Enter second String\n";
    cin.getline(second, 100);
  
    if(isAnagram(first, second)){
        cout << "Both strings are Anagram";
    } else {
        cout << "Both strings are not Anagram";
    }
     
    return 0;
}
  
/*
 * Function to check whether two strings are anagram or not
 * returns 1 if anagram otherwise 0
 */
int isAnagram(char *first, char *second){
    int firstCounter[256] = {0}, secondCounter[256] = {0};
    int i;
    // The length of two strings must be equal
    if(strlen(first) != strlen(second)){
        return 0;
    }
     
    // Count frequency of characters of first String 
    for(i = 0; first[i] != '\0'; i++){
        firstCounter[first[i]]++;
    }
     
    // count frequency of characters of second String
    for(i = 0; second[i] != '\0'; i++){
        secondCounter[second[i]]++;
    }
    // Character count of both strings must be equal, 
    // If not equal return 0, otherwise 1 */
    for(i = 0; i < 256; i++){
        if(firstCounter[i] != secondCounter[i])
            return 0;
    }
     
    return 1;
}

Output

Enter first String
orange
Enter second String
anorge
Both strings are Anagram
Enter first String
orange
Enter second String
apple
Both strings are not Anagram

In above program, we first take two strings as input from user and store it in character array input and output. Here we wrote a function called “isAnagram” to check whether two strings are anagram or not. isAnagram functions implements the above mentioned algorithm to check anagram string.

We call isAnagram function by passing two input strings and based on the response of function we print appropriate message on screen using cout.

Exploring basic to advanced concepts in the C++ programming language helps beginners and experienced programmers to excel in C++ Programs. Go with this page & enhance your coding skills.