Created
May 20, 2011 18:19
-
-
Save spilth/983470 to your computer and use it in GitHub Desktop.
Piglating Java Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.buildndeploy.piglatin; | |
public class Translator { | |
public String translate(String input) { | |
Character firstLetter = input.charAt(0); | |
String newString; | |
if (isVowel(firstLetter)) { | |
newString = input + "way"; | |
} else { | |
int splitIndex = findSplitIndex(input); | |
String newFirstHalf = input.substring(splitIndex); | |
String newSecondHalf = input.substring(0,splitIndex).toLowerCase(); | |
Character newFirstLetter = newFirstHalf.charAt(0); | |
if (Character.isUpperCase(firstLetter)) { | |
newFirstLetter = Character.toUpperCase(newFirstLetter); | |
} | |
newString = newFirstLetter + newFirstHalf.substring(1) + newSecondHalf + "ay"; | |
} | |
return newString; | |
} | |
private int findSplitIndex(String input) { | |
int splitIndex = 0; | |
int inputLength = input.length(); | |
for (int i = 0; i < inputLength; i++) { | |
if (isVowel(input.charAt(i))) { | |
splitIndex = i; | |
break; | |
} | |
} | |
return splitIndex; | |
} | |
private boolean isVowel(Character firstLetter) { | |
return firstLetter.equals('a') | |
|| firstLetter.equals('e') | |
|| firstLetter.equals('i') | |
|| firstLetter.equals('o') | |
|| firstLetter.equals('u') | |
|| firstLetter.equals('y') | |
|| firstLetter.equals('A') | |
|| firstLetter.equals('E') | |
|| firstLetter.equals('I') | |
|| firstLetter.equals('O') | |
|| firstLetter.equals('U') | |
|| firstLetter.equals('Y'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment