Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Rafal Bielech
- //CSCI 401
- //Ch 7 homework Anagram
- import java.util.Scanner;
- import java.util.Arrays;
- public class Anagram {
- public static boolean isAnagram(String a, String b){
- //method to convert string to all lower case
- String a1= a.toLowerCase();
- String b1= b.toLowerCase();
- //convert string to char array
- char[] arra = a1.toCharArray();
- char[] arrb = b1.toCharArray();
- //sort it alphabetically
- Arrays.sort(arra);
- Arrays.sort(arrb);
- //method that checks if there are same character at the same places in 2 arrays , if not sorted then might yield false
- boolean result = Arrays.equals(arra, arrb);
- return result;}
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner input = new Scanner (System.in);
- System.out.println("Enter the first string:");
- String a = input.nextLine();
- System.out.println("Enter the second string:");
- String b = input.nextLine();
- //if result true, print this //if result false, print this
- System.out.print(isAnagram(a,b)? "The two strings are anagrams": "The two strings are not anagrams");
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement