Search Tutorials

Tuesday 23 April 2013

Java code to find the pigLatin equivalent of a word

Java program to find the pigLatin equivalent of a word.

import java.io.*;
class PigLatin
{
static String n;
static int l;
 public static void main(String args[]) throws IOException
 {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 System.out.print("Enter a word : ");
 n = br.readLine();
 l = n.length();
 System.out.println(toPigLatin());
 }
 public static boolean isVowel(char c)
 {
 String vowels[] = {"a","e","i","o","u"};
 for(int i=0;i<5;i++)
 {
  if(Character.toString(c).equalsIgnoreCase(vowels[i]))
   return true;
 }
 return false;
 }
 public static String toPigLatin()
 {
 int f=-1;
 char c;
 for(int i=0;i<l;i++)
 {
  // Find the location of the 1st vowel, if any
  c = n.charAt(i);
  if(isVowel(c))
  {
   f = i;
   break;
  }
 }
 
 if(f==0 || f==-1)
  return n;
 else
 {
  // Store part after the vowel
  String b = n.substring(f).toUpperCase();
  // Store part before the vowel
  String d = n.substring(0,f).toUpperCase();
  return b+d+"AY";
 }
 }
}

ALGORITHM:-

1. Start

2. Accept a sentence from the user.

3. Break the word at the first occurring vowel.

5. Set flag f = 1

6. Add the deleted letters to the end of the word.

7. Add “AY” at the end.

8. if(f ==1) then print the changed String.
else
Print the original String.

9. End

OUTPUT:-

Java code to find the pigLatin equivalent of a word

Enter a word : Mayank

AYANKMAY

Related Programs:-

Check whether a string is a Palindrome or not

Check the number of Uppercase letters, Lowercase letters, numerals, vowels, spaces & special characters in a string

Read the date, day and year

Read a Date and give the day number

Multiply two Matrices

No comments:

Post a Comment

Back to Top