Javatpoint Logo
Javatpoint Logo

Pig Latin Program in Java

In this section, we will learn what is Pig Latin word and how to translate or encode a word into a Pig Latin word. Also, we will implement the logic in a JavaM program to find the Pig Latin string.

What is Pig Latin?

Pig Latin is a language game in English in which words are altered according to the rules. It is also known as Igpay Atinlay. The given word encrypted into the same language.

Example: Ingstray, emay, oorflay, etc. are the Pig Latin words.

Pig Latin Program in Java

The Rule for Converting a Word or String into Pig Latin

There may be the following scenarios:

  • Word may begin with a consonant
  • Word may begin with a vowel
  • Word may begin with a consonant cluster
  • Word may have no vowel

If the Word Begin with Consonant

If the word begins with a consonant, all the alphabets before the vowel is placed at the end of the word. After that, the suffix ay is added to the word. For example, consider the following words:

duck = uckd = uckd + ay = uckday

banana = ananab = ananab + ay = ananabay

happy = appyh = appyh + ay = appyhay

If the Word Begin with Vowel

If the word begins with a vowel, in such cases yay is added at the end of the word. Some people also add way or hay or other endings. For example, consider the following words:

I: I + yay= iyay

always: always + yay = alwaysyay or always + way = alwaysway

egg: egg + yay = eggyay or egg + way = eggway

If the Word Begin with Consonant Clusters

The word that has more than one consonant at the beginning is known as consonant clusters words. In other words, the multiple consonants that form one sound are known as consonant clusters. In such a case, the whole sound (ay) is added to the end of the word. For example, consider the following words:

store: orest = orest + ay = orestay

smile: ilesm = ilesm + ay = ilesmay

glove: ovegl = ovegl + ay = oveglay

If the Word has no Vowel

The Pig Latin word is not possible if the input word or string contains no vowel. For example, Pig Latin of the word nymphly is not possible.

Note: To make the string Pig Latin there must at least one vowel in the word.

Java Pig Latin Program

Let's implement the logic in the Java program to find the Pig Latin word.

PigLatinExample.java

Output:

utterbay
appleay
emay
ildchay
eggay
orestay
eunoiaay
-1 

In the above output, -1 denotes the string or word that has no vowel.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA