Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package workshop06;
- import java.util.Arrays;
- /**
- *
- * @author NgT
- */
- public class Ex3 {
- static void isAnagram(String s1, String s2) {
- String copyOfs1 = s1.replaceAll("\\s", "");
- String copyOfs2 = s2.replaceAll("\\s", "");
- boolean status = true;
- if (copyOfs1.length() != copyOfs2.length()) {
- status = false;
- } else {
- char[] s1Array = copyOfs1.toLowerCase().toCharArray();
- char[] s2Array = copyOfs2.toLowerCase().toCharArray();
- Arrays.sort(s1Array);
- Arrays.sort(s2Array);
- status = Arrays.equals(s1Array, s2Array);
- }
- if (status) {
- System.out.println(s1 + " and " + s2 + " are anagrams");
- } else {
- System.out.println(s1 + " and " + s2 + " are not anagrams");
- }
- }
- public static void main(String[] args) {
- isAnagram("parliament", "partial men,");
- isAnagram("software", "swear oft.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment