Piglatin Word Program in Java and Python
ICSE 2013 Piglatin word solution to convert a word to uppercase, rearrange it from the first vowel and add AY.
Question:
Write a program that encodes a word into Piglatin.
To translate a word into a Piglatin word, convert the word into uppercase and then place the first vowel of the original word as the start of the new word along with the remaining alphabets. The alphabets present before the vowel are shifted towards the end followed by AY.
Example 1
Example 2
Algorithm:
Step 1: Start.
Step 2: Create a Scanner object to accept input from the user.
Step 3: Accept a word and store it in the string variable s.
Step 4: Convert s to uppercase.
Step 5: Store the length of s in l.
Step 6: Initialize pos to -1 to show that no vowel has been found yet.
Step 7: Run a loop from index 0 to l - 1.
Step 8: Extract the current character from the word.
Step 9: If the current character is A, E, I, O or U, store its index in pos and stop the loop.
Step 10: If pos is not -1, extract the part of the word from pos to the end.
Step 11: Extract the part of the word before pos.
Step 12: Join the two extracted parts in the order first-vowel part + before-vowel part + AY.
Step 13: Display the Piglatin word.
Step 14: If no vowel is found, display that Piglatin conversion is not possible.
Step 15: Stop.
Explanation:
Piglatin conversion in this program depends on the position of the first vowel in the word. The word is first converted to uppercase because the required output is in capital letters. This also makes vowel checking easier. Once the word is uppercase, the program needs to compare each character with only A, E, I, O and U. There is no need to check lowercase vowels separately.
The variable pos is used to store the index of the first vowel. It is initialized with -1, which is not a valid index in Java strings. This value acts as a signal that no vowel has been found yet. The loop starts from the first character and moves towards the end of the word. As soon as a vowel is found, its position is stored in pos and the loop is stopped using break. Stopping immediately is important because Piglatin uses the first vowel, not any later vowel.
If a vowel is found, the word is divided into two parts. The first part begins from the first vowel and continues up to the end of the word. This is obtained using substring(pos). The second part contains all letters before the first vowel and is obtained using substring(0, pos). The Piglatin word is then formed by joining the first part, the second part and the suffix AY.
For example, in LONDON, the first vowel is O at index 1. The part from the vowel is ONDON, and the part before it is L. Joining them with AY gives ONDONLAY. In OLYMPICS, the first letter is already a vowel, so the first part is the whole word and the second part is empty. Therefore the result becomes OLYMPICSAY. If the word has no vowel, the program prints that Piglatin conversion is not possible.
Java Program:
/**
* The class Piglatin inputs a word and translates it into a Piglatin word.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ICSE 2013 Question 6
*/
import java.util.Scanner;
class Piglatin
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter any word: ");
String s = sc.nextLine();
s = s.toUpperCase();
int l = s.length();
int pos = -1;
// Find the position of the first vowel.
for(int i = 0; i < l; i++)
{
char ch = s.charAt(i);
if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
pos = i;
break;
}
}
if(pos != -1)
{
// Rearrange the word from the first vowel and add AY.
String a = s.substring(pos);
String b = s.substring(0, pos);
String pig = a + b + "AY";
System.out.println("The Piglatin of the word = " + pig);
}
else
{
System.out.println("No vowel, hence piglatin not possible");
}
}
}Equivalent Python Program:
# Program to convert a word into Piglatin.
word = input("Enter any word: ")
word = word.upper()
pos = -1
# Find the position of the first vowel.
for i in range(len(word)):
if word[i] in "AEIOU":
pos = i
break
if pos != -1:
# Rearrange the word from the first vowel and add AY.
piglatin = word[pos:] + word[:pos] + "AY"
print("The Piglatin of the word =", piglatin)
else:
print("No vowel, hence piglatin not possible")Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.