Java program to accept a word and convert it to piglatin form

Java program to accept a word and convert it to piglatin form(trouble --- oubletray)

RULS: (source: https://en.wikipedia.org/wiki/Pig_Latin#:~:text=For%20words%20that%20begin%20with,%22latin%22%20%3D%20%22atinlay%22 )

For words that begin with consonant sounds, all letters before the initial vowel are placed at the end of the word sequence. Then, "ay" is added, as in the following examples:[12]

  • "pig" = "igpay"
  • "latin" = "atinlay"
  • "banana" = "ananabay"

When words begin with consonant clusters (multiple consonants that form one sound), the whole sound is added to the end when speaking or writing.[13]

  • "friends" = "iendsfray"
  • "smile" = "ilesmay"
  • "string" = "ingstray"

For words that begin with vowel sounds, one just adds "hay", "way" or "yay" to the end. Examples are:

  • "eat" = "eatway"
  • "omelet" = "omeletway"
  • "are" = "areway"

An alternative convention for words beginning with vowel sounds, one removes the initial vowel(s) along with the first consonant or consonant cluster. This usually only works for words with more than one syllable and offers a more unique variant of the words in keeping with the mysterious, unrecognizable sounds of the converted words. Examples are:

  • "every" = "eryevay"
  • "omelet" = "eletomay"
  • "another" = "otheranay"

# What I have done:
I simply followed the above rule, and add "ay" as required.

CODE:

import java.util.Scanner;

public class demo{

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter the String: ");

String str = sc.nextLine();

sc.close();

String temp = "";

String strArray[] = str.split(" ");

for(int i=0;i<strArray.length;i++) {

for(int x=0;x<strArray[i].length();x++){

if(vowel(strArray[i].charAt(x))) {

if(x==0)

temp = strArray[i] + "ay";

else

temp = subString(strArray[i],x);

strArray[i] = temp;

break;

}

}

}

str ="";

for(String x : strArray)

str = str + x + " ";

System.out.print(str);

}

static boolean vowel(char ch) {

boolean b = false;

switch(Character.toUpperCase(ch)) {

case('A'):

b = true;

break;

case('E'):b = true;

break;

case('I'):

b = true;

break;

case('O'):

b = true;

break;

case('U'):

b = true;

break;

}

return b;

}

static String subString(String str,int start) {

String sub = "";

for(int i=start;i<str.length();i++)

sub = sub + str.charAt(i);


for(int i=0;i<start;i++)

sub = sub + str.charAt(i);

sub = sub.concat("ay");

return sub;

}

}

Comments