pig latin java program

Pig latin Rules:

If the word starting with vowel then we have to append “way” to that word.
Eg: ice – iceway

If the word starting with consonents then we have to remove the consonent letters and append the removed word along with ay at the end.
Eg: java – avajay

Program:

package com.javadomain.in;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Piglatin {

	public static void main(String[] args) throws IOException {
		String input = RandomWordGenerator();
		System.out.println("Generated random_word "+input);

		String pig_latin_no_vowel = "ay";
		String pig_lating_vowel = "way";
		String comma_separate =",";
		Map letter_location = new HashMap();

		for(int i=0;i<input.length();i++){
			letter_location.put(i,input.charAt(i));
		}

		int vowel_first_letter = vowel_start_location(input,letter_location);
		//System.out.println(vowel_first_letter);

		String vowel_based_split_first_half = input.substring(0, vowel_first_letter);
		String vowel_based_split_second_half = input.substring(vowel_first_letter+1, input.length());
		char first_vowel = input.charAt(vowel_first_letter);
		//System.out.println("first vowel "+first_vowel);
		//System.out.println("before vowel "+vowel_based_split_first_half);
		//System.out.println("after vowel "+vowel_based_split_second_half);

		boolean isFirstLetterVowel = isStartswithVowelLetters(input);
		//System.out.println("main function boolean "+isFirstLetterVowel);

		if(isFirstLetterVowel){
			System.out.println("Pig Latin of the input is "+input + pig_lating_vowel);
		}else
		{
			System.out.println("Pig Latin of the input is "+ first_vowel + vowel_based_split_second_half + vowel_based_split_first_half + pig_latin_no_vowel);
		}

	}

	public static int vowel_start_location (String word,Map letter_location){
		int location = 0;

		for(int j=0;j<letter_location.size();j++){
			if(letter_location.get(j).toString().equalsIgnoreCase("a")||letter_location.get(j).toString().equalsIgnoreCase("E")||letter_location.get(j).toString().equalsIgnoreCase("I")||letter_location.get(j).toString().equalsIgnoreCase("O")||letter_location.get(j).toString().equalsIgnoreCase("U")){
				int char_location = letter_location.toString().charAt(j);
				location = j;
				break;
			}
			else{
				continue;
			}
		}

		return location;
	}

	public static boolean isStartswithVowelLetters (String word){
		boolean isstartswithvowel = false;

		String first_letter = word.substring(0, 1).toString();

			if(first_letter.toString().equalsIgnoreCase("a")||first_letter.toString().equalsIgnoreCase("e")||first_letter.toString().equalsIgnoreCase("i")||first_letter.toString().equalsIgnoreCase("o")||first_letter.toString().equalsIgnoreCase("u")){
				isstartswithvowel = true;
			}
			else{
				isstartswithvowel = false;
			}
		return isstartswithvowel;

	}

	public static String RandomWordGenerator () throws IOException{
		Document doc = Jsoup.connect("http://creativitygames.net/random-word-generator/randomwords/1").timeout(0).get();
		Elements name_character = doc.select("li#randomword_1");
		//System.out.println(name_character.text().toString());
		return name_character.text().toString();
	}

}

Output:
Generated random_word resting
Pig Latin of the input is estingray
 

Thanks for reading this post………!!!

672 total views, 1 views today

Leave a Reply

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