String Anagram Program In C | Easy Explanation

So, In this post, we are going to discuss the program and algorithm of anagram In C/C++/Java. What matters is only the algorithm, If you better understand the Algorithm you can code in any language of your choice?

What is Anagram

Two words are said to be anagram of each other if

  1. Count of letters in both words are same or you can say the length of both the word must be same
  2. Letters present in word A must be present in word B equally, but order of letter can be changed.


Geeks and eegks are anagram of each other

Code and doce are anagram of each other

code and docd are not anagram because all the words in the First string are not in the second string.

Problem Statement:

Check Whether two strings are anagram of each other

Method 1:

  • Sort both the strings.
  • if the length of string A is not equal to string B then return false
  • If length is equal then check each first character of string A with the first character of String B and so on, if a mismatch happen return False
  • At the end of the function return true


Let’s String A = “geeks” and String B =”eekgs”

  1. Step 1: Sort both string and then A = “eegks” and string B = “eegks”
  2. length of both the string is the same hence, move on step 3
  3. we start comparing each character and we do not find any mismatch.
  4. In the end, we will return true.

Anagram Program In C


#include <bits/stdc++.h> 
using namespace std; 

/* function for checkins string */
bool checkAnagram(string str1, string str2) 
	// Get lengths of both strings 
	int n1 = str1.length(); 
	int n2 = str2.length(); 


	// Step - 1 Sort both the strings  
	sort(str1.begin(), str1.end()); 
	sort(str2.begin(), str2.end()); 
  // Step -2  If length of both strings is not same, then they 
	// cannot be anagram 
	if (n1 != n2) 
		return false; 

	// Step - 3 Compare sorted strings 
	for (int i = 0; i < n1; i++) 
		if (str1[i] != str2[i]) 
			return false; 

	return true; 

// Driver code 
int main() 
	string str1 = "geeks"; 
	string str2 = "eeksg"; 
	if (checkAnagram(str1, str2)) 
		cout << "The two strings are anagram "; 
		cout << "The two strings are not anagram"; 

	return 0; 

Method 2:

This method is better than Method 1 and complete in less time complexity. In this method, we used the count technique to store the occurrence of each character and then subtract the occurrence of a character in second-string. Wait, a complete explanation is below.

  1. Create an integer array of size 26[we are assuming only small alphabets]
  2. Traverse string A and add an occurrence of all characters in the array
  3. Traverse string B and subtract the occurrence of all characters in the array.


Let String A =”tocode” and String B =”cotode”. We creater an integer array of size 26

int array[26] = {0};

We traverse the string A and add occurence like this

for(int i = 0;i<A.length;i++) { array[A[i] – ‘a’]++; }

Now, array contain the occurrence of each character of String A

Now, we traverse the string B and start subtracting the occurrence of characters like this

for(int i = 0;i<B.length;i++) {array[B[i]]-‘a’}–;

Now We check the complete array and if any index of the array contains any value other than 0 then Given strings are not an anagram.

anagram program in C

Anagram program In Java

/*package whatever //do not write package name here */


class GFG {
    public static boolean checkAnagram(String a, String b)
        int[] arr = new int[26];
        for(int i = 0;i<a.length();i++)
            arr[a.charAt(i) - 'a']++;
        for(int i = 0;i<b.length();i++)
            arr[b.charAt(i) - 'a']--;
        for(int i = 0;i<arr.length;i++)
            return false;
        return true;
	public static void main (String[] args) {

Check the code link here and run it. optional – getline in C

Leave a Comment

Your email address will not be published. Required fields are marked *