[go: up one dir, main page]

0% found this document useful (0 votes)
39 views1 page

Dsa Assignment

This Java code defines an Anagram class with a main method to check if two input strings are anagrams of each other. It converts the strings to lowercase, checks they are the same length, converts them to character arrays, sorts the arrays, and compares them to see if they are equal, printing the result.

Uploaded by

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

Dsa Assignment

This Java code defines an Anagram class with a main method to check if two input strings are anagrams of each other. It converts the strings to lowercase, checks they are the same length, converts them to character arrays, sorts the arrays, and compares them to see if they are equal, printing the result.

Uploaded by

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

import java.util.

Arrays;
public class Anagram {
public static void main (String [] args) {
String str1="Brag";
String str2="Grab";

//Converting both the string to lower case.


str1 = str1.toLowerCase();
str2 = str2.toLowerCase();

//Checking for the length of strings


if (str1.length() != str2.length()) {
System.out.println("Both the strings are not anagram");
}
else {
//Converting both the arrays to character array
char[] string1 = str1.toCharArray();
char[] string2 = str2.toCharArray();

//Sorting the arrays using in-built function sort ()


Arrays.sort(string1);
Arrays.sort(string2);

//Comparing both the arrays using in-built function equals ()


if(Arrays.equals(string1, string2) == true) {
System.out.println("Both the strings are anagram");
}
else {
System.out.println("Both the strings are not anagram");
}
}
}
}

You might also like