SHARE
TWEET

Untitled

a guest Jan 24th, 2020 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package edu.wit.cs.comp1050;
  2. import java.util.*;
  3. public class LA1a {
  4.    
  5.     public static void main(String[] args) {
  6.         Scanner input = new Scanner(System.in);
  7.        
  8.         System.out.print("Enter phrase 1: ");
  9.         String word1 = input.nextLine();
  10.         System.out.print("Enter phrase 2: ");
  11.         String word2 = input.nextLine();
  12.        
  13.         int[] counter1 = countLetters(word1);
  14.         int[] counter2 = countLetters(word2);
  15.        
  16.        
  17.         boolean egg = sameCounts(counter1, counter2);
  18.  
  19.         if(egg==true) {System.out.println("These phrases are anagrams.");}
  20.         else {System.out.println("These phrases are not anagrams.");}
  21.    
  22.     }
  23.    
  24.     public static int[] countLetters (String word) {
  25.        
  26.         char letter;
  27.         int[] count = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  28.        
  29.    
  30.        
  31.         for(int i=0; i < word.length(); i++) {
  32.             letter = word.charAt(i);
  33.            
  34.             if(letter=='a' || letter=='A') {count[0]++;}
  35.             if(letter=='b' || letter=='B') {count[1]++;}
  36.             if(letter=='c' || letter=='C') {count[2]++;}
  37.             if(letter=='d' || letter=='D') {count[3]++;}   
  38.             if(letter=='e' || letter=='E') {count[4]++;}
  39.             if(letter=='f' || letter=='F') {count[5]++;}
  40.             if(letter=='g' || letter=='G') {count[6]++;}   
  41.             if(letter=='h' || letter=='H') {count[7]++;}
  42.             if(letter=='i' || letter=='I') {count[8]++;}
  43.             if(letter=='j' || letter=='J') {count[9]++;}
  44.             if(letter=='k' || letter=='K') {count[10]++;}
  45.             if(letter=='l' || letter=='L') {count[11]++;}
  46.             if(letter=='m' || letter=='M') {count[12]++;}
  47.             if(letter=='n' || letter=='N') {count[13]++;}
  48.             if(letter=='o' || letter=='O') {count[14]++;}
  49.             if(letter=='p' || letter=='P') {count[15]++;}
  50.             if(letter=='q' || letter=='Q') {count[16]++;}
  51.             if(letter=='r' || letter=='R') {count[17]++;}
  52.             if(letter=='s' || letter=='S') {count[18]++;}
  53.             if(letter=='t' || letter=='T') {count[19]++;}
  54.             if(letter=='u' || letter=='U') {count[20]++;}
  55.             if(letter=='v' || letter=='V') {count[21]++;}
  56.             if(letter=='w' || letter=='W') {count[22]++;}
  57.             if(letter=='x' || letter=='X') {count[23]++;}
  58.             if(letter=='y' || letter=='Y') {count[24]++;}
  59.             if(letter=='z' || letter=='Z') {count[25]++;}
  60.             }
  61.  
  62.  
  63.        
  64.         return count;
  65.        
  66.     }
  67.     public static boolean sameCounts(int[] array1, int[] array2) {
  68.      boolean egg= true;
  69.      int j=0;
  70.        
  71.      for(int i=0; i!=26; i++) {
  72.            
  73.         if(array1.length==array2.length){              
  74.                                                     //  if(Arrays.equals(array1, array2)) {
  75.             if(array1[j]!=array2[j])
  76.             {egg=false;}
  77.             j++;
  78.         }
  79.         }
  80.         return egg;
  81.     }
  82.  
  83. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top