C++ Program to Check Strings are Anagram or Not

Here is 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 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;
}
 
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.


Recommended Posts
C++ Program to Check Whether a Character is Vowel or Not
C++ Program to Check Whether a Character is Alphabet or Not
C++ Program to Check Whether Number is Even or Odd
C++ Program to check Whether a Number is Palindrome or Not
C++ Program to find length of string
C++ Program to concatenate two strings without using strcat function.
C++ Program to Copy String Without Using strcpy
C++ Program to Find the Frequency of Characters in a String
C++ Program to Count Number of Vowels, Consonant, and Spaces in String
C++ Program to Count Words in Sentence
C++ Program to Print Pascal Triangle
All C++ Programs