Skip to content

Create Anagrammanipulatorr.java #146

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions JAVA/Anagrammanipulatorr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.Arrays;

public class Anagrammanipulatorr {

public static boolean areAnagrams(String str1, String str2) {
// Remove spaces,convert both strings to lowercase comparison
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();

// Check if lengths of two strings are different
if (str1.length() != str2.length()) {
return false;
}

// Convertstrings to char arrays and sort
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();

Arrays.sort(charArray1);
Arrays.sort(charArray2);

// Compare sorted arrays
return Arrays.equals(charArray1, charArray2);
}

public static void main(String[] args) {
String str1 = "abcd";
String str2 = "dabc";

if (areAnagrams(str1, str2)) {
System.out.println(str1 + " and " + str2 + " are anagrams.");
} else {
System.out.println(str1 + " and " + str2 + " are not anagrams.");
}
}
}