Pig Latin is nothing but a language game in which an English word is jumbled according to a simple set of rules. It takes the first consonant, or a consonant cluster (if more than one consonant is found), moves it to the end of the word, and adds "ay" to it.
For example, PIG becomes IGPAY when written in Pig Latin form, and TRUCK becomes UCKTRAY.
Below is the program written in Java that can convert a given English word to its Pig Latin form. This program is very popular in ICSE.
import java.io.*;
class Piglatin{
public static void main(String args[])throws IOException{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.print("Enter a word:");
String s=br.readLine();
s=s.toUpperCase();
char ch='\u0000';
int index=0;
String str=new String();
outer:
for(int i=0;i<s.length();i++){
ch=s.charAt(i);
switch(ch){
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
index=i;
break outer;
}
}
str=str+s.substring(index)+s.substring(0,index)+"AY";
System.out.println(str);
}
}
No comments:
Post a Comment