// Remove spaces and convert to lowercase for accurate comparison
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
// Check if the lengths of the strings are equal
if (str1.length() != str2.length()) {
return false;
}
// Convert strings to char arrays and sort them
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
// Compare sorted char arrays
return Arrays.equals(charArray1, charArray2);
}
public static void main(String[] args) {
// Example usage
String word1 = "listen";
String word2 = "silent";
if (areAnagrams(word1, word2)) {
System.out.println(word1 + " and " + word2 + " are anagrams.");
} else {
System.out.println(word1 + " and " + word2 + " are not anagrams.");
}
}
}
```
This program defines a function `areAnagrams` that takes two strings, removes spaces and converts them to lowercase, sorts the characters, and then checks if the sorted arrays are equal. The `main` method provides an example of how to use this function with two words.
import java.util.Arrays;
Certainly! Here's a simple Java program to check if two strings are anagrams:
// Remove spaces and convert to lowercase for accurate comparison
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
// Check if the lengths of the strings are equal
if (str1.length() != str2.length()) {
return false;
}
// Convert strings to char arrays and sort them
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
// Compare sorted char arrays
return Arrays.equals(charArray1, charArray2);
}
public static void main(String[] args) {
// Example usage
String word1 = "listen";
String word2 = "silent";
if (areAnagrams(word1, word2)) {
System.out.println(word1 + " and " + word2 + " are anagrams.");
} else {
System.out.println(word1 + " and " + word2 + " are not anagrams.");
}
}
}
```
This program defines a function `areAnagrams` that takes two strings, removes spaces and converts them to lowercase, sorts the characters, and then checks if the sorted arrays are equal. The `main` method provides an example of how to use this function with two words.
Answers & Comments
Answer:
import java.util.Arrays;
public class AnagramChecker {
public static boolean areAnagrams(String str1, String str2) {
// Remove spaces and convert to lowercase for case-insensitive comparison
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
// Check if the lengths are equal
if (str1.length() != str2.length()) {
return false;
}
// Convert strings to char arrays and sort them
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
// Compare the sorted char arrays
return Arrays.equals(charArray1, charArray2);
}
public static void main(String[] args) {
String word1 = "listen";
String word2 = "silent";
if (areAnagrams(word1, word2)) {
System.out.println(word1 + " and " + word2 + " are anagrams.");
} else {
System.out.println(word1 + " and " + word2 + " are not anagrams.");
}
}
}
Answer:
Certainly! Here's a simple Java program to check if two strings are anagrams:
```java
import java.util.Arrays;
public class AnagramChecker {
// Function to check if two strings are anagrams
static boolean areAnagrams(String str1, String str2) {
// Remove spaces and convert to lowercase for accurate comparison
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
// Check if the lengths of the strings are equal
if (str1.length() != str2.length()) {
return false;
}
// Convert strings to char arrays and sort them
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
// Compare sorted char arrays
return Arrays.equals(charArray1, charArray2);
}
public static void main(String[] args) {
// Example usage
String word1 = "listen";
String word2 = "silent";
if (areAnagrams(word1, word2)) {
System.out.println(word1 + " and " + word2 + " are anagrams.");
} else {
System.out.println(word1 + " and " + word2 + " are not anagrams.");
}
}
}
```
This program defines a function `areAnagrams` that takes two strings, removes spaces and converts them to lowercase, sorts the characters, and then checks if the sorted arrays are equal. The `main` method provides an example of how to use this function with two words.
import java.util.Arrays;
Certainly! Here's a simple Java program to check if two strings are anagrams:
```java
import java.util.Arrays;
public class AnagramChecker {
// Function to check if two strings are anagrams
static boolean areAnagrams(String str1, String str2) {
// Remove spaces and convert to lowercase for accurate comparison
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
// Check if the lengths of the strings are equal
if (str1.length() != str2.length()) {
return false;
}
// Convert strings to char arrays and sort them
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
// Compare sorted char arrays
return Arrays.equals(charArray1, charArray2);
}
public static void main(String[] args) {
// Example usage
String word1 = "listen";
String word2 = "silent";
if (areAnagrams(word1, word2)) {
System.out.println(word1 + " and " + word2 + " are anagrams.");
} else {
System.out.println(word1 + " and " + word2 + " are not anagrams.");
}
}
}
```
This program defines a function `areAnagrams` that takes two strings, removes spaces and converts them to lowercase, sorts the characters, and then checks if the sorted arrays are equal. The `main` method provides an example of how to use this function with two words.
public class AnagramChecker {
// Function to check if two strings are anagrams
static boolean areAnagrams(String str1, String str2) {
// Remove spaces and convert to lowercase for accurate comparison
str1 = str1.replaceAll("\\s", "").toLowerCase();
str2 = str2.replaceAll("\\s", "").toLowerCase();
// Check if the lengths of the strings are equal
if (str1.length() != str2.length()) {
return false;
}
// Convert strings to char arrays and sort them
char[] charArray1 = str1.toCharArray();
char[] charArray2 = str2.toCharArray();
Arrays.sort(charArray1);
Arrays.sort(charArray2);
// Compare sorted char arrays
return Arrays.equals(charArray1, charArray2);
}
public static void main(String[] args) {
// Example usage
String word1 = "listen";
String word2 = "silent";
if (areAnagrams(word1, word2)) {
System.out.println(word1 + " and " + word2 + " are anagrams.");
} else {
System.out.println(word1 + " and " + word2 + " are not anagrams.");
}
}
}