BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Check two Strings are anagram or not

By Chaitanya Singh | Filed Under: Java Examples

Anagram of a string is another string with the same characters but order of the characters can be different. For example, Two strings “Listen” and “Silent” are anagram strings as both contain same characters, just the order of the characters is different. Similarly Strings “Race” and “Care” are also anagrams. In this article, you will learn how to write a java program to check two strings are anagram or not.

Program to determine whether two strings are anagram

Steps:

  1. Convert the given strings in the lowercase letters using toLowerCase() method, this is to perform case insensitive comparison.
  2. Compare the lengths (find length using length() method) of the given strings, if lengths are not equal then strings cannot be anagram else proceed to next step.
  3. Copy the characters of the strings into an array using toCharArray() method.
  4. Sort the arrays using Arrays.sort(). This is to sort the characters in the array in a particular order as the anagram strings characters are in different order. For example, the arrays of strings “race” and “care” are {‘r’, ‘a’, ‘c’, ‘e’} & {‘c’, ‘a’, ‘r’, ‘e’} receptively. After sorting both the arrays are same : {‘a’, ‘c’, ‘e’, ‘r’}
  5. Compare the sorted arrays, if the arrays are equal then the strings are anagrams else they are not anagram strings.
import java.util.Arrays;
public class JavaExample {
  public static void main (String [] args) {
    String str1="Race";
    String str2="Care";

    //Converting the strings to lower case to
    //perform case insensitive comparison
    str1 = str1.toLowerCase();
    str2 = str2.toLowerCase();

    //Comparing the length of the two strings if the lengths
    //are not equal then strings cannot be anagrams
    if (str1.length() != str2.length()) {
      System.out.println("Given strings are not anagram.");
    }
    else {
      //copying the characters of each strings in two different arrays
      char[] arrayOfStr1 = str1.toCharArray();
      char[] arrayOfStr2 = str2.toCharArray();

      //Sorting both the arrays using Arrays.sort()
      Arrays.sort(arrayOfStr1);
      Arrays.sort(arrayOfStr2);

      //Comparing the sorted arrays, if equal anagram else not anagram
      if(Arrays.equals(arrayOfStr1, arrayOfStr2) == true) {
        System.out.println("Given Strings are anagram.");
      }
      else {
        System.out.println("Given Strings are not anagram.");
      }
    }
  }
}

Output:

Given Strings are anagram.

Related Java Examples

  1. Java program to compare two strings
  2. Java program to count vowels and consonants in a string
  3. Java program to count the occurrence of a character in a string
  4. Java program to perform bubble sort on strings
❮ Java TutorialJava Programs ❯

Leave a Reply Cancel reply

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

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap