Hurry! Try our new Interactive Courses for FREE. 🥳   🚀
Written By:
Sonali7
5 minute read
AnagramJavaC LanguageHowTo

What is Anagram? (Program in Java/C)

Posted in Programming   LAST UPDATED: SEPTEMBER 13, 2021

Have you ever come across the term Anagram and are wondering what it is? If yes, then don't worry because in this tutorial, we will see what an anagram is and an example to check whether two strings are anagram or not in two different programming languages.

So let's get started.

What is an Anagram?

An Anagram is a word or a phrase that is formed by rearranging the letters and words of a different word or a phrase. For example, the word dormitory itself can be arranged into a dirty room also the word conversation can be converted into voices rant on. So, in simple terms, we can say that a word or phrase made by transposing the letters of another word or phrase is known as an anagram.

Here, the original word is termed as the subject of anagram, and the word that is produced after rearranging the letters of the original word is termed as an anagram.

Anagram example

Let us look at the examples to check whether two strings are anagram or not.

Check Whether Anagram or Not In C

In this example, we will see how to check whether two strings are anagram or not in C.

Algorithm:

  1. Start
  2. Declare two character arrays.
  3. Initialize both the array.
  4. Declare variables to store the length.
  5. Check whether the length of the two strings is equal or not.
  6. If equal, then use two for loops to check whether the strings are anagram or not.
  7. Else print that both the strings are not an anagram.
  8. Display the result.
  9. Stop.

Let us look at the below example for a better understanding of the above algorithm.


#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char str1[20]="care";
    char str2[20]="race";
    int len, len1, len2, i, j, flag=0, nflag=0;
    len1 = strlen(str1);
    len2 = strlen(str2);
    if(len1 == len2)
    {
        len = len1;
        for(i=0; i<len; i++)
        {
            flag = 0;
            for(j=0; j<len; j++)
            {
                if(str1[i] == str2[j])
                {
                    flag = 1;
                    break;
                }
            }
            if(flag == 0)
            {
                nflag = 1;
                break;
            }
        }
        if(nflag == 1)
            printf("\nStrings are not Anagram");
        else
            printf("\nStrings are Anagram");
    }
    else
        printf("\nBoth string must contain same number of character to be an Anagram Strings");
    return 0;
}


Strings are Anagram

Check Whether Anagram or Not in Java

In this example, we will see how to check whether two strings are anagram or not in java.

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare two strings.
  4. Ask the user to initialize these variables.
  5. Convert the two strings into lower case.
  6. Now, convert the strings to a character array.
  7. Call a boolean-type user-defined function to check anagram.
  8. Take both the character array as parameters.
  9. First, find the length of both the character arrays.
  10. Now, sort both the arrays.
  11. Using a for loop check whether the two anagrams are equal or not.
  12. Return the boolean result.
  13. Display the output
  14. Stop.

Let us look at the below example for a better understanding of the above algorithm.

//Java program to check whether two strings are anagrams of each other
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.*;

public class Main 
{
	static boolean areAnagram(char[] str1, char[] str2)
	{
		int n1 = str1.length;
		int n2 = str2.length;
    	if (n1 != n2)
			return false;
        Arrays.sort(str1);
		Arrays.sort(str2);
        for (int i = 0; i < n1; i++)
			if (str1[i] != str2[i])
				return false;
		return true;
	}
	public static void main(String args[])
	{
	    Scanner sc=new Scanner(System.in);
	    String str1=sc.nextLine();
	    String str2=sc.nextLine();
	    str1 = str1.toLowerCase();
        str2 = str2.toLowerCase();
        System.out.println("The entered string is: "+str1);
        System.out.println("The entered string is: "+str2);
		char[] charArray1 = str1.toCharArray();
        char[] charArray2 = str2.toCharArray();;
		if (areAnagram(charArray1, charArray2))
			System.out.println("The two strings are anagram of each other");
		else
			System.out.println("The two strings are not anagram of each other");
	}
}


The entered string is: study
The entered string is: dusty
The two strings are an anagram of each other.

Conclusion:

So, in this article, we saw what an anagram is and an example to check whether two strings are anagram or not in two different programming languages.

You may also like:


IF YOU LIKE IT, THEN SHARE IT

RELATED POSTS