Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class PirateTranslator
- {
- public static int search(String [] a, String x)
- {
- int low = 0;
- int hi = a.length - 1;
- int index = -1;
- while(low <= hi)
- {
- int mid = (low + hi) / 2;
- if(x.compareTo(a[mid]) == -1)
- {
- hi = mid - 1;
- }
- else if(x.compareTo(a[mid]) == 1)
- {
- low = mid + 1;
- }
- else
- {
- index = mid;
- break;
- }
- }
- return index;
- }
- public static void main(String [] args)
- {
- String [] english = {"hello","hi","pardon me","excuse me","my","friend","sir","madam","miss","stranger","where",
- "is","the","you","tell","know","how far","old","attractive","happy","nearby","restroom","restaurant","hotel",
- "pub","bank",};
- String [] pirate = {"ahoy","yo-ho-ho","avast", "arrr", "me","me bucko", "matey", "proud beauty", "comely wench",
- "scurvy dog","whar","is", "be","th'","ye","be tellin'", "be knowin'","how many leagues","barnacle-covered",
- "comely","grog-filled","broadside", "head","galley","fleabag inn", "Skull & Scuppers","buried treasure"};
- System.out.println("enter a string:");
- String x = Console.readString();
- System.out.println(x + " is at index ");
- System.out.println(search(english, x));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment