English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I want to make a method that will mix up words in this pattern:
For example:
String "What's" becomes At'swhay (this is usually called the piglatin)
I have found the method to re arrange the words that way but the problem is that if the original words starts with a capital then the transformed word must also starts with a capital. I've tried many different ways using the String method toUpperCase, but it wont work for a char (the first letter of the beginning of the word). Does any one know how to develop this algorithm?

2006-10-29 18:16:21 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

Find below an example for converting to PigLatin:-
/*********************************************/

public class PigLatin {

private boolean isVowel(char c)
{
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}

private int indexOfFirstVowel(String s)
{
for (int i = 0; i < s.length(); i++)
{
if (isVowel(s.charAt(i)))
{
return i;
}
}
return -1;
}

public String toPigLatin(String s)
{
int firstVowel = indexOfFirstVowel( s.toLowerCase( ) );
if (firstVowel == -1)
return s;
if (firstVowel == 0)
return s + "way";
String head = s.substring(0, firstVowel);
String tail = s.substring(firstVowel);
String result = tail + head + "ay";
return result;
}

public String convertLine(String line) {
String words[] = line.split("\\b");
String result = "";
for (int i = 0; i < words.length; i++)
{
result = result + toPigLatin( words[i] );
}
return result;
}

public static void main(String [] args)
{
PigLatin pigLatin = new PigLatin();
String originalString = "Text to be converted to PigLatin";
String pigLatinString = pigLatin.convertLine( originalString );
System.out.println(" originalString : " + originalString);
System.out.println(" pigLatinString : " + pigLatinString);
}

}
/*********************************************/

2006-10-29 23:10:10 · answer #1 · answered by mashiur1973 2 · 0 0

After converting to PigLatin, use String method toLowerCase(). Then, get the char arrray, change the first char to uppercase using Character.toUpperCase(), then create new String.

String pigLatin = ...;
char[] characters = pigLatin.toCharArray();
characters[0] = Character.toUpperCase(characters[0]);
pigLatin = new String(characters);

2006-10-31 02:53:15 · answer #2 · answered by vincentgl 5 · 0 0

fedest.com, questions and answers