Signup/Sign In

What are Anagram in Java and C?

Posted in Programming   LAST UPDATED: MAY 2, 2023

    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 anagrams in Java and C or not in two different programming languages.

    So let's get started.

    What are Anagrams in Java?

    An Anagram is a word or a phrase that is formed by rearranging the letters and words of a different word or 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 an 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 anagrams or not.

    Check Whether Anagram or Not In C

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

    Algorithm:

    1. Start
    2. Declare two character arrays.
    3. Initialize both 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 anagrams 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 Anagrams in Java or Not

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

    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 lowercase.
    6. Now, convert the strings to a character array.
    7. Call a boolean-type user-defined function to check the anagram.
    8. Take both the character array as parameters.
    9. First, find the length of both the character arrays.
    10. Now, sort both 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: the study
    The entered string is: dusty
    The two strings are an anagram of each other.

    Conclusion:

    Anagrams in Java are a fascinating and important topic in computer programming.

    Being able to recognize and manipulate anagrams can be useful in many contexts, such as in cryptography, data compression, and natural language processing.

    By implementing an anagram program in Java, you can not only sharpen your programming skills but also explore the intriguing world of wordplay and language manipulation. So why not give it a try and see what anagrams in Java can reveal to you? Happy coding!

    Frequnetly Asked Questions(FAQs)

    1. What is anagram C program?

    An anagram C program is a program written in the C programming language that determines if two input strings are anagrams or not.

    2. How to write anagram in C?

    To write an anagram program in C, you can sort the characters of each string and compare them. If the sorted strings are equal, then the input strings are anagrams.

    3. How to make anagram in Java?

    To make an anagram program in Java, you can use a similar approach as in C, i.e. sort the characters of each string and compare them. Alternatively, you can count the occurrence of each character in both strings and compare the counts.

    4. What is an anagram number example?

    An anagram number is a number whose digits can be rearranged to form a different number. For example, 123 and 231 are anagrams of each other, so both are anagram numbers.

    You may also like:

    About the author:
    I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.
    Tags:CJavaanagrams
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS