Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Anagrams {
- public static void main(String[] args) {
- String s1, s2;
- try (Scanner sc = new Scanner(System.in)) {
- System.out.println("Enter the two words: ");
- s1 = sc.nextLine().toLowerCase().replace(" ", "");
- s2 = sc.nextLine().toLowerCase().replace(" ", "");
- }
- if (s1.length() == s2.length() && isAnagram(s1, s2)) {
- System.out.format("%s is an anagram of %s.", s1, s2);
- } else {
- System.out.format("'%s' and '%s' are not anagrams.", s1, s2);
- }
- }
- public static boolean isAnagram(String s1, String s2) {
- return (sumOfChars(s1) == sumOfChars(s2));
- }
- public static int sumOfChars(String s) {
- int sum = 0;
- for (char c : s.toCharArray()) {
- sum += c;
- }
- return sum;
- }
- }
Add Comment
Please, Sign In to add comment