Advertisement
KevinNT03

Pig Latin Translator

Feb 24th, 2020
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5.     /**
  6.      *This program takes a string given by the user and  converts it to pig latin
  7.      */
  8.     public static void main(String[] args) {
  9.         System.out.println("Please insert a sentence to be translated"); //asks the user for input
  10.         Scanner scan = new Scanner(System.in);
  11.         String original = scan.nextLine();
  12.  
  13.         String translation = translateSentence(original); //translates it using a function
  14.  
  15.         System.out.println(translation); //prints the result
  16.     }
  17.  
  18.     /**
  19.      * This translates a whole sentence by using translateWord
  20.      * @param str - user input
  21.      * @return result, translated sentence
  22.      */
  23.     public static String translateSentence(String str){
  24.         int lastSpace = 0;
  25.         String result = "";
  26.  
  27.         for(int x = 0; x < str.length(); x++){
  28.             if(str.charAt(x) == ' '){ //separates words based on spaces
  29.                 String word = str.substring(lastSpace, x);
  30.                 String translation = translateWord(word);
  31.  
  32.                 result = result + translation + " "; //adds translated word to the sentence
  33.  
  34.                 lastSpace = x + 1;
  35.             }else if(x == str.length() - 1){ //last word in the string
  36.                 String word = str.substring(lastSpace, x + 1);
  37.                 String translation = translateWord(word);
  38.  
  39.                 result = result + translation + " "; //adds translated word to the sentence
  40.  
  41.                 lastSpace = x + 1;
  42.             }
  43.         }
  44.  
  45.         return result;
  46.     }
  47.  
  48.     /**
  49.      * Translates a single word
  50.      * @param str - the word to be translated
  51.      * @return result - translated string
  52.      */
  53.     public static String translateWord(String str){
  54.         String result;
  55.         String endPart;
  56.  
  57.         for(int x = 0; x < str.length(); x++){ //goes through the string looking for vowels
  58.             if(isVowel(str.charAt(x))){
  59.                 endPart = str.substring(0, x); //if there is a vowel, that's the limit
  60.                 result = str.substring(x);
  61.  
  62.                 endPart = endPart + "ay";      //add the part before the vowel with an "ay" to the end
  63.                 result = result + "-" + endPart;
  64.  
  65.                 return result;
  66.             }
  67.         }
  68.  
  69.         //if there isn't a vowel, check for "y"
  70.         for(int x = 0; x < str.length(); x++){
  71.             if(str.charAt(x) == 'y'||str.charAt(x) == 'Y'){
  72.                 endPart = str.substring(0, x);
  73.                 result = str.substring(x);
  74.  
  75.                 endPart = endPart + "ay";
  76.                 result = result + "-" + endPart;
  77.  
  78.                 return result;
  79.             }
  80.         }
  81.  
  82.         //if there aren't any vowels or "y", just add "-ay"
  83.         result = str + "-ay";
  84.         return result;
  85.     }
  86.  
  87.     /**
  88.      * Returns whether a character is a vowel
  89.      * @param b the character to be analyzed
  90.      * @return if it is a vowel
  91.      */
  92.     public static boolean isVowel(char b){
  93.         if(b == 'a'||b == 'e'||b == 'i'||b == 'o'||b == 'u'||b == 'A'||b == 'E'||b == 'I'||b == 'O'||b == 'U'){
  94.             return true;
  95.         }else{
  96.             return false;
  97.         }
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement