NikP23

PirateTranslator

Dec 2nd, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.io.*;
  2. public class PirateTranslator
  3. {
  4.   public static int search(String [] a, String x)
  5.   {
  6.     int low = 0;
  7.     int hi = a.length - 1;
  8.     int index = -1;
  9.     while(low <= hi)
  10.     {
  11.       int mid = (low + hi) / 2;
  12.       if(x.compareTo(a[mid]) == -1)
  13.       {
  14.         hi = mid - 1;
  15.       }
  16.       else if(x.compareTo(a[mid]) == 1)
  17.       {
  18.         low = mid + 1;
  19.       }
  20.       else
  21.       {
  22.         index = mid;
  23.         break;
  24.       }
  25.     }
  26.    
  27.     return index;
  28.   }
  29.  
  30.   public static void main(String [] args)
  31.   {
  32.     String [] english = {"hello","hi","pardon me","excuse me","my","friend","sir","madam","miss","stranger","where",
  33.       "is","the","you","tell","know","how far","old","attractive","happy","nearby","restroom","restaurant","hotel",
  34.       "pub","bank",};
  35.     String [] pirate = {"ahoy","yo-ho-ho","avast", "arrr", "me","me bucko", "matey", "proud beauty", "comely wench",
  36.       "scurvy dog","whar","is", "be","th'","ye","be tellin'", "be knowin'","how many leagues","barnacle-covered",
  37.       "comely","grog-filled","broadside", "head","galley","fleabag inn", "Skull & Scuppers","buried treasure"};
  38.    
  39.     System.out.println("enter a string:");
  40.     String x = Console.readString();
  41.    
  42.     System.out.println(x + " is at index ");
  43.     System.out.println(search(english, x));
  44.   }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment