ISC Practicals and Theory - Piglatin
Updated 21 Oct 2026
Pig Latin is a word game in which every English word is rewritten by a fixed rule. This program, written in the main method alone, reads a sentence that ends with a full stop, question mark or exclamation mark, converts every word to Pig Latin and prints the new sentence with the same terminator. The words are cut out of the sentence with charAt() and substring() only, with no split() or StringTokenizer.
Definition
The rule used here, with the vowels A, E, I, O, U:
- If a word begins with a vowel, add
AYat the end. - Otherwise move the leading consonants, up to the first vowel, to the end of the word and then add
AY.
Examples:
OPENbegins with a vowel, so it becomesOPENAY.THE: the consonantsTHmove to the end, givingE+TH+AY=ETHAY.QUICK: the first vowel isU, so onlyQmoves, givingUICKQAY. (QUis not treated as a pair.)RHYTHMhas no vowel at all. The whole word would move to the end, so it comes out asRHYTHMAY.
Decisions made in this program:
- The sentence is converted to upper case with
toUpperCase()and the output is in upper case. Yis not a vowel, soWHYbecomesWHYAYandTRYbecomesTRYAY.- A word with no vowel gets
AYadded to the unchanged word. - The array holds at most 50 words; extra words are ignored.
- Characters other than letters (commas, digits) stay inside the word and are treated as consonants.
Algorithm
- Read the sentence, remove the spaces at its two ends with
trim()and convert it to capitals withtoUpperCase(). It is valid only if it has at least 2 characters and ends in.,?or!; otherwise printINVALID INPUTand stop. - Cut out the words: for
ifrom 0 tolen - 2(before the terminator), a character that is not a space is added to the wordw; a space after a word storeswin the arraywords(at most 50 words) and makeswempty. After the loop the last word is stored too. - Convert each word
xof the array. Findp, the index of its first vowel (A,E,I,OorU), by skipping the leading consonants.- If
p == 0, the word begins with a vowel: addAYat its end. - Otherwise move the part before the first vowel to the end and add
AY:x.substring(p) + x.substring(0, p) + "AY". If the word has no vowel at all,pequals its length, so the whole word moves and the result is the word followed byAY.
- If
- Join the converted words with single spaces and add the terminator at the end. Print the sentence.
Program
The first for loop in main fills the array words, and the second converts each word and joins the result in res.
import java.util.*;
public class PigLatin
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a sentence:");
String sent = sc.nextLine().trim().toUpperCase(); // the sentence (upper case, trimmed)
int len = sent.length();
boolean valid = false; // true if the sentence ends with . ? or !
if (len >= 2)
{
char last = sent.charAt(len - 1); // terminator of the sentence
valid = last == '.' || last == '?' || last == '!';
}
if (!valid)
{
System.out.println("INVALID INPUT");
return;
}
String words[] = new String[50]; // the words of the sentence, one per element (at most 50)
int count = 0; // number of words stored in the array
String w = ""; // word being built
for (int i = 0; i < len - 1; i++) // stop before the terminator
{
char ch = sent.charAt(i);
if (ch != ' ')
w = w + ch; // letter: add to current word
else if (!w.equals("")) // space after a word: store it
{
if (count < words.length)
words[count++] = w;
w = "";
}
}
if (!w.equals("") && count < words.length) // last word
words[count++] = w;
String res = ""; // converted sentence
for (int i = 0; i < count; i++)
{
String x = words[i]; // the word being converted
int p = 0; // index of the first vowel
while (p < x.length() && "AEIOU".indexOf(x.charAt(p)) < 0)
p++; // skip leading consonants
if (p == 0)
res = res + x + "AY"; // begins with a vowel
else
res = res + x.substring(p) + x.substring(0, p) + "AY"; // p == length: the whole word moves
if (i < count - 1)
res = res + " ";
}
res = res + sent.charAt(len - 1); // add '.', '?' or '!'
System.out.println(res);
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
sent | String | The sentence typed by the user, in capitals and trimmed |
len | int | Length of sent |
valid | boolean | True if the sentence has enough characters and ends with ., ? or ! |
last | char | The last character of the sentence, the terminator |
words | String[] | The words of the sentence, one per element |
count | int | Number of words stored in words |
w | String | The word being built while cutting out the words |
i | int | Loop counter over the characters, then over the words |
ch | char | The character being examined |
res | String | The converted sentence |
x | String | The word being converted |
p | int | Index of the first vowel of x |
Sample input and output
Sample 1
Enter a sentence:
Open the door.
OPENAY ETHAY OORDAY.Sample 2
Enter a sentence:
The quick brown fox is here!
ETHAY UICKQAY OWNBRAY OXFAY ISAY EREHAY!Sample 3
Enter a sentence:
Why try rhythm?
WHYAY TRYAY RHYTHMAY?Sample 4
Enter a sentence:
Hello world
INVALID INPUTDry run
Input Open the door. After the input is read, sent = OPEN THE DOOR. (14 characters). The sentence is valid as the last character is .. The word-cutting loop runs for i = 0 to 12 (len = 13).
i | ch | w after the step | count |
|---|---|---|---|
| 0 to 3 | O, P, E, N | OPEN | 0 |
| 4 | space | stored in words[0], w = empty | 1 |
| 5 to 7 | T, H, E | THE | 1 |
| 8 | space | stored in words[1], w = empty | 2 |
| 9 to 12 | D, O, O, R | DOOR | 2 |
| after loop | DOOR stored in words[2] | 3 |
Now each word is converted.
| Word | p | Result |
|---|---|---|
OPEN | 0 (O is a vowel) | OPEN + AY = OPENAY |
THE | 2 (T, H skipped, E is a vowel) | E + TH + AY = ETHAY |
DOOR | 1 (D skipped, O is a vowel) | OOR + D + AY = OORDAY |
The words are joined with single spaces and the terminator . is added, so res = OPENAY ETHAY OORDAY., the same as the real run above.
Variations
- Reverse each word "Write a program to accept a sentence and reverse every word of it, keeping the order of the words."
- Longest word "Write a program to accept a sentence and display the number of words and the longest word."
- Capitalise words "Write a program to accept a sentence and change the first letter of every word to upper case."
- Pig Latin using recursion "Convert a word to Pig Latin using a recursive method."