0% found this document useful (0 votes)
13 views2 pages

Java Record 10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Java Record 10

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING

Ex no: 10
ANAGRAMCHECKER
Date:

Question
An anagram is a word or a phrase made by transposing the letters of another word or phrase; for
example, "parliament" is an anagram of "partial men," and "software" is an anagram of "swear oft."
Write a Java program that figures out whether one string is an anagram of another string. The
program should ignore white space and punctuation.

Aim
To write a Java code for checking the given string is anagram or not.

Code
package anagram; import
java.util.Arrays; public class
AnagramChecker { public static void
main(String[] args) {
String str1 = "parliament";
String str2 = "partial men";
System.out.println("ROLL NO:717823P106");
System.out.println("NAME:ASHWANTH M" );
if (areAnagrams(str1, str2)) {
System.out.println("\"" + str1 + "\" and \"" + str2 + "\" are anagrams.");
} else {
System.out.println("\"" + str1 + "\" and \"" + str2 + "\" are not anagrams.");
}} public static boolean areAnagrams(String str1, String str2) { String
cleanedStr1 = cleanString(str1); String cleanedStr2 = cleanString(str2); if
(cleanedStr1.length() != cleanedStr2.length()) {
return false; } char[] charArray1 = cleanedStr1.toCharArray(); char[] charArray2
=cleanedStr2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2); return
Arrays.equals(charArray1, charArray2); }
public static String cleanString(String str) { return
str.replaceAll("[\\W]", "").toLowerCase(); }}

26 717823P106
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING

Output

Result
Thus, the Java code for checking the given string is anagram or not has been successfully
developed and the output was verified.

27 717823P106

You might also like