Java Program to Encoding a word into Pig Latin

Introduction

This program helps to encode an input word to Pig Latin. Pig Latin is a way to encode a word. A Pig Latin is an encrypted word in English, which is generated by doing the following alterations:

The first vowel occurring in the input word is placed at the start of the new word along with the remaining alphabets of it. The alphabets present before the first vowel are shifted at the end of the new word followed by “ay”.
Example: Suppose, We are a String “Dharmendra” and want to encode to Pig Latin. Pig Latin of “Dharmendra” will be “armendraDhay”.
The word “Dharmendra” is having the first vowel as ‘a’ and string will start from ‘armendra’ and ‘Dh’ will be added at the end of the string followed by ‘ay’.

Java Program

This program is written using the java technologies with a very basic logic. There may be different logic which will be more efficient.

package com.kw.sample;

import java.util.Scanner;

/**
 * This Program help to Convert input String to PigLatin String.
 * 
 * @author dsahu1
 * 
 */
public class KWConvertStrToPigLatin {

	// Instantiate Object Scanner to take Input from the user
	private static Scanner scan = new Scanner(System.in);
	// Attribute to hold blank String
	private static String BLANK_STRING = "";
	// Attribute to hold empty String
	private static String EMPTY_STRING = " ";

	private static String PIG_LATEN_STRING = "ay";

	/**
	 * This method helps to execute the Program.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println("Enter the String to Convert Pig Latin String : ");
		// Attribute to contain String1
		String str1 = scan.nextLine();
		// Attribute to hold Pig Latin String
		String pigLatinString = BLANK_STRING;
		// Attribute to hold valid String boolean value true or false
		boolean isValidString = false;
		// Validate String Null or empty
		if (str1 != null && !str1.trim().isEmpty()) {
			// Call method to convert String to Pig Latin String
			pigLatinString = converntStrToPigLatinString1(str1);
			isValidString = true;
		}
		// Check valid string and Print the output
		if (isValidString) {

			// Print Original String
			System.out.println("Original String :: " + str1);

			// Print Pig Latin String
			System.out.println("Pig Latin String :: " + pigLatinString);
		} else {
			// Print Error String
			System.out
					.println(" **** There is an issue with the Input String! ****");
		}

	}

	/**
	 * This method helps to convert Original String to Pig Latin String.
	 * Example: Original String :: Dharmendra Kumar Sahu Pig Latin String ::
	 * armendraDhay umarKay ahuSay
	 * 
	 * @param str
	 * @return return String
	 */
	private static String converntStrToPigLatinString(String str) {
		// Instantiate String to hold final result
		String outputStr = BLANK_STRING;
		// Split the String into Array of words
		String[] wordArray = str.split(EMPTY_STRING);
		// Initialize count
		int count = 0;
		// Iterate Array which contain word(s)
		for (String word : wordArray) {

			// Increase the counter
			count++;
			// Assign -1 to index
			int index = -1;
			// Iterate the word by character
			for (int i = 0; i < word.length(); i++) {
				// check vowel and set the index
				if (isVowel(word.charAt(i))) {
					index = i;
					break;
				}
			}
			String tempString = BLANK_STRING;

			if (index != -1) {
				// Construct the Pig Latin String
				tempString = word.substring(index) + word.substring(0, index)
						+ PIG_LATEN_STRING;
			} else {
				tempString = word;
			}
			outputStr += tempString;

			if (count != wordArray.length) {

				outputStr += EMPTY_STRING;
			}
		}
		// Return the output result
		return outputStr;

	}

	/**
	 * ************* NOTE ALTERNATE Implementation Using
	 * StringBuilder************ This method helps to convert Original String to
	 * Pig Latin String. Example: Original String :: Dharmendra Kumar Sahu Pig
	 * Latin String :: armendraDhay umarKay ahuSay
	 * 
	 * @param str
	 * @return return String
	 */
	private static String converntStrToPigLatinString1(String str) {

		// Instantiate String Builder to hold final result
		StringBuilder outputStr = new StringBuilder();
		// Split the String into Array of words
		String[] wordArray = str.split(EMPTY_STRING);

		// Initialize count
		int count = 0;
		// Iterate Array which contain word(s)
		for (String word : wordArray) {
			// Increase the counter
			count++;
			// Assign -1 to index
			int index = -1;
			// Iterate word by character and apply the logic
			for (int i = 0; i < word.length(); i++) {
				// check vowel and set the index
				if (isVowel(word.charAt(i))) {
					index = i;
					break;
				}
			}

			StringBuilder tempStringBuilder = new StringBuilder(BLANK_STRING);

			if (index != -1) {
				// Construct the Pig Latin String
				tempStringBuilder.append(word.substring(index))
						.append(word.substring(0, index))
						.append(PIG_LATEN_STRING);
			} else {
				tempStringBuilder.append(word);
			}
			outputStr.append(tempStringBuilder);

			if (count != wordArray.length) {

				outputStr.append(EMPTY_STRING);
			}
		}
		// Return the output result
		return outputStr.toString();
	}

	/**
	 * This method helps to check vowel character.
	 * 
	 * @param c
	 * 
	 * @return return true or false based on vowel character
	 */
	private static boolean isVowel(char c) {

		return (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'
				|| c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
	}

}

Output

Enter the String to Convert Pig Latin String :
Dharmendra Kumar Sahu
Original String :: Dharmendra Kumar Sahu
Pig Latin String :: armendraDhay umarKay ahuSay

Enter the String to Convert Pig Latin String :
The monkey is jumping
Original String :: The monkey is jumping
Pig Latin String :: eThay onkeymay isay umpingjay

Leave a Reply

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