Home / Blue J / Java program to translate english to piglatin form

Java program to translate english to piglatin form

This is a program to convert a english word into piglatin form:

Piglatin form: Any word is converted into piglatin form by starting the word from the first vowel and adding the remaining part in the end followed by “AY”

import java.io.*;
	class piglatin
	{
		public static void main(String args[])throws Exception
		{	
			String a;
			char c;int i,l;
			System.out.println("Enter a word");
 			BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
			a=br.readLine();
			l=a.length();
			for(i=0;i<l;i++)
			{	c=a.charAt(i);
				if(c=='a' | c=='e' | c=='i' | c=='o' | c=='u')
					break;

			}
			a=a.substring(i,l)+a.substring(0,i)+"AY"; // converting a word to Piglatin form
			System.out.println("Pig Latin form for the  word is: "+a);
		}
	}

OUTPUT:

converting a word into piglatin form
Java program for Piglatin form

Another Java program to convert whole english sentence to piglatin form:

import java.io.*;
class piglatin
{
	public static void main(String args[])throws Exception
	{	
		String a,ans=" ",temp;
		char c;int i,l,k=0;
		System.out.println("Enter english sentence to convert to piglatin");
		BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
		a=br.readLine();
		a+=" ";			// last word doesnt have a space' ' it will be difficult to select
		l=a.length();
		for(i=0;i<l;i++)
		{
		c=a.charAt(i); 
		if(c=='a' | c=='e' | c=='i' | c=='o' | c=='u')
		{	temp=a.substring(k,i);			// for selecting a word from a sentence
			k=a.indexOf(" ",i);
			ans+=a.substring(i,k)+temp+"AY ";	// for converting from english to piglatin form
			i=k;	//to reduce the iteration
			k++;			
		}
		}

		System.out.println("Pig Latin form for the  word is: "+ans);
	}	
}

OUTPUT:

Also See:   Java Program to check for a Kaprekar number
Program to convert english to piglatin
Converting english into piglatin form

However, conversion of piglatin to english is not possible as you have to know the length of the word else you cant interpret the position of vowel in a word and roll back the changes in piglatin word. Thus latin to english translation is not possible if you dont have a latin dictionary.

Tags: Program in java for piglatin word, write a program in java to print piglatin form of a given word.

Check Also

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can ...

One comment

  1. Harshit Sir,

    The second code for Piglatin doesn’t generate a space between the words. I tried to find the missing link. You will need to replace line 19)ans+=a.substring(i,k)+temp+”AY “; with ans+=a.substring(i,k)+temp+”AY “+ ” “;

    By the way, thumbs up for your help and logic to this program. Finally I found it in your site.

Leave a Reply

Your email address will not be published. Required fields are marked *