Monday, 6 January 2014

Java: Piglatin

package piglatin2;
import java.util.Scanner;
/**
 *
 * @author ravikiran
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String statement, result, another;
      piglatin translator = new piglatin();

      do
      {
         System.out.println ();
         System.out.println ("Enter a sentence (no punctuation):");
         Scanner input= new Scanner(System.in);
         statement = input.nextLine();

         System.out.println ();
         result = translator.translate (statement);
         System.out.println ("That sentence in Pig Latin is:");
         System.out.println (result);

         System.out.println ();
         System.out.print ("Translate another sentence (y/n)? ");
         another = input.nextLine();
      }
      while (another.equalsIgnoreCase("y"));
    }

}

package piglatin2;
import java.util.StringTokenizer;
/**
 *
 * @author ravikiran
 */
public class piglatin extends Main{
    public String translate(String statement)
{
String string=null;

StringTokenizer input=new StringTokenizer(statement);
while(input.hasMoreTokens())
{
string=string+translator(input.nextToken());
string=string+" ";
}
return string;


}
public String translator(String word )
{
String string=null;
if(!vowel(word))
{

string=word+"ay";

}
else
{
string=word.substring(1) + word.substring(0,1) + "ay";
}
return string;
}

private static boolean vowel (String word)
{
String Vowels = "aeiou";

char letter = word.charAt(0);

return (Vowels.indexOf(letter) != -1);
}

}

No comments:

Post a Comment