Monday, August 20, 2018

Java Program to check two strings are Anagram or not



An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “LISTEN” and “SILENT” are anagram of each other.



package com.caots.test;

import java.util.Arrays;

public class Test {
public static void main(String[] args) {
//String s1="LISTEN", s2="SILENT";
String s3="TRIANGLE", s4="INTEGRAL";
boolean flag=checkAnagram(s3, s4);
if(flag==true)
System.out.println(s3+" & "+s4+" are Anagram strings");
else
System.out.println("No Anagram strings");
}

public static boolean checkAnagram(String s1,String s2){
char[] crr1=s1.toCharArray();
char[] crr2=s2.toCharArray();
Arrays.sort(crr1);
Arrays.sort(crr2);
if(Arrays.equals(crr1, crr2))
return true;
return false;
}
}







Java Program to check two strings are Anagram or not
Java Program to check two strings are Anagram or not




Share this

0 Comment to "Java Program to check two strings are Anagram or not"

Post a Comment