Tag Archives: string

Piglatin

Piglatin or Pig Latin is a constructed language game where words in English are altered according to a simple set of rules. Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an ay (for example, pig yields igpay, banana yields ananabay, and trash yields ashtray).Piglatin

The objective is to conceal the meaning of the words from others not familiar with the rules. The reference to Latin is a deliberate misnomer, as it is simply a form of jargon, used only for its English connotations as a “strange and foreign-sounding language.”

Rules for converting a word into piglatin

The usual rules for changing standard English into Pig Latin are as follows:

For words that begin with consonant sounds, the initial consonant or consonant cluster is moved to the end of the word, and “ay” is added, as in the following examples:

  • “happy” → “appyhay”
  • “duck” → “uckday”
  • “glove” → “oveglay”

For words that begin with vowel sounds or silent letter, you just add ay (or sometimes “way”) to the end. Examples are:

  • “egg” → “eggay” (or “eggway”)
  • “inbox” → “inboxay” (or “inboxway”)
  • “eight” → “eightay” (or “eightway”)

The program to convert a word into piglatin (pig latin) is as follows:

class Piglatin{
    public static boolean isVowel( char ch ){
        return ("aeiouAEIOU").indexOf( ch ) >= 0 ;
    }
    public static String wordToPiglatin( String word ){
        word=word.toLowerCase();
        int length = word.length();
        char ch;
        String piglatin="";
        for( int i = 0 ; i < length ; i++ ){
            ch = word.charAt( i );
            if( isVowel( ch ) ){
                piglatin = word.substring(i) + word.substring(0, i) + "ay";
                break;
            }
        }
        return piglatin;
    }
    public static void main( String []args ){
        System.out.println( wordToPiglatin( "KING" ) );
        System.out.println( wordToPiglatin( "TROUBLE" ) );
        System.out.println( wordToPiglatin( "happy" ) );
        System.out.println( wordToPiglatin( "duck" ) );
        System.out.println( wordToPiglatin( "glove" ) );
        System.out.println( wordToPiglatin( "egg" ) );
        System.out.println( wordToPiglatin( "inbox" ) );
        System.out.println( wordToPiglatin( "eight" ) );
    }
}

The above program for converting a word into piglatin (pig latin) essentially consists of finding the occurrence of first vowel and then split and rearrange the string after splitting at the point of occurrence. The conversion of string to lower-case is for aesthetic purpose.

As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.