A simple program to check anagrams in java

This is a simple program in Java. This program can be used to check if the two given strings are anagram. Two given strings are said to be anagrams when they have equal length and same characters are present in both the Strings. An example of anagram strings is SILENT and LISTEN !

Screenshot


The Code:
import java.util.*;

public class Anagram
{
 public static boolean isAnagram(String s1,String s2)
 {
  if(s1.length()!=s2.length())
  {  return false; }
  s1=sortCharacters(s1);
  
  s2=sortCharacters(s2);
  return s1.equals(s2);
  
  
 }
 public static String sortCharacters(String str)
 {
  char[] c=str.toLowerCase().toCharArray();
  Arrays.sort(c);
  return String.valueOf(c);
 }
 public static void main(String arg[])
 {
  String s1,s2;
  Scanner sc=new Scanner(System.in);
  System.out.println("enter 2 strings");
  s1=sc.nextLine();
  s2=sc.nextLine();
  if(isAnagram(s1,s2))
   System.out.println(s1+"is an anagram"+s2);
  else
   System.out.println(s1+"is not an anagram"+s2);
 }
}

Got anything to say, please do comment here.

Comments

  1. 20+ Off The Edge Titanium For Sale
    It's a great looking titanium meaning stone edge piece titanium tv apk for smith titanium sale in Arizona. They are beautifully 토토 사이트 made and have a very long lasting durability. dewalt titanium drill bit set Rating: 5 · ‎1 review

    ReplyDelete

Post a Comment

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java