Guest User

Untitled

a guest
Jan 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Anagrams {
  4. public static void main(String[] args) {
  5. String s1, s2;
  6. try (Scanner sc = new Scanner(System.in)) {
  7. System.out.println("Enter the two words: ");
  8. s1 = sc.nextLine().toLowerCase().replace(" ", "");
  9. s2 = sc.nextLine().toLowerCase().replace(" ", "");
  10. }
  11. if (s1.length() == s2.length() && isAnagram(s1, s2)) {
  12. System.out.format("%s is an anagram of %s.", s1, s2);
  13. } else {
  14. System.out.format("'%s' and '%s' are not anagrams.", s1, s2);
  15. }
  16. }
  17.  
  18. public static boolean isAnagram(String s1, String s2) {
  19. return (sumOfChars(s1) == sumOfChars(s2));
  20. }
  21.  
  22. public static int sumOfChars(String s) {
  23. int sum = 0;
  24. for (char c : s.toCharArray()) {
  25. sum += c;
  26. }
  27. return sum;
  28. }
  29. }
Add Comment
Please, Sign In to add comment