Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched.
Examples
pigIt('Pig latin is cool'); // igPay atinlay siay oolcay pigIt('Hello world !'); // elloHay orldway !
public class PigLatin {
public static String pigIt(String str) {
// Write code here
if(str.length() == 0)return "";
if(str.length() == 1)return str.concat("ay");
int i = 0, j = i+1;
StringBuilder sb = new StringBuilder();
while(j < str.length()) {
if(str.charAt(j) == ' ') {
sb.append(str.substring(i+1, j)).append(str.substring(i, i+1)).append("ay ");
//str = str.substring(j+1);
i = j+1;
j = i+1;
}else {
j++;
}
}
Character ch = new Character(str.charAt(i));
if(Character.isAlphabetic(str.charAt(i))) {
sb.append(str.substring(i+1, j)).append(str.substring(i, i+1)).append("ay");
//str = str.substring(j+1);
i = j+1;
j = i+1;
}else {
sb.append(str.substring(i, j));
}
return sb.toString();
}
}
Test Cases:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
import java.lang.StringBuilder;
import java.util.Random;
public class PigLatinTests {
@Test
public void FixedTests() {
assertEquals("igPay atinlay siay oolcay", PigLatin.pigIt("Pig latin is cool"));
assertEquals("hisTay siay ymay tringsay", PigLatin.pigIt("This is my string"));
assertEquals("ctaAay steay abulafay", PigLatin.pigIt("Acta est fabula"));
assertEquals("arbaBay onnay acitfay hilosophumpay", PigLatin.pigIt("Barba non facit philosophum"));
assertEquals("ctaAay steay abulafay", PigLatin.pigIt("Acta est fabula"));
assertEquals("ucullusCay onnay acitfay onachummay", PigLatin.pigIt("Cucullus non facit monachum"));
assertEquals("uraDay exlay edsay exlay", PigLatin.pigIt("Dura lex sed lex"));
assertEquals("rrareEay umanumhay steay", PigLatin.pigIt("Errare humanum est"));
assertEquals("luctuatFay ecnay ergiturmay", PigLatin.pigIt("Fluctuat nec mergitur"));
assertEquals("uttaGay avatcay apidemlay", PigLatin.pigIt("Gutta cavat lapidem"));
assertEquals("icHay teay uncnay", PigLatin.pigIt("Hic et nunc"));
assertEquals("nIay inovay eritasvay", PigLatin.pigIt("In vino veritas"));
assertEquals("uxLay niay enebristay ucetlay", PigLatin.pigIt("Lux in tenebris lucet"));
assertEquals("orituriMay olumusnay orimay", PigLatin.pigIt("Morituri nolumus mori"));
assertEquals("orituriMay olumusnay orimay", PigLatin.pigIt("Morituri nolumus mori"));
assertEquals("uncNay steay ibendumbay", PigLatin.pigIt("Nunc est bibendum"));
assertEquals("Oay emporatay oay oresmay !", PigLatin.pigIt("O tempora o mores !"));
assertEquals("anemPay teay ircensescay", PigLatin.pigIt("Panem et circenses"));
assertEquals("uisQay ustodietcay psosiay ustodescay ?", PigLatin.pigIt("Quis custodiet ipsos custodes ?"));
assertEquals("equiescatRay niay acepay", PigLatin.pigIt("Requiescat in pace"));
assertEquals("icSay ransittay loriagay undimay", PigLatin.pigIt("Sic transit gloria mundi"));
assertEquals("imeoTay anaosDay teay onaday erentesfay", PigLatin.pigIt("Timeo Danaos et dona ferentes"));
assertEquals("ltimaUay ecatnay", PigLatin.pigIt("Ultima necat"));
assertEquals("eniVay idivay icivay", PigLatin.pigIt("Veni vidi vici"));
}
private static final String join(String delim, String... parts) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < parts.length - 1; i++) {
builder.append(parts[i]).append(delim);
}
if (parts.length > 0) {
builder.append(parts[parts.length - 1]);
}
return builder.toString();
}
public static String generateRandomChars(String candidateChars, int length) {
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(candidateChars.charAt(random.nextInt(candidateChars.length())));
}
return sb.toString();
}
public static String C(String Z) {
return Z.replaceAll("(\\w)(\\w*)(\\s|$)", "$2$1ay$3");
}
@Test
public void RandomTests() {
Random random = new Random();
for (int i = 0; i < 100; i++) {
String[] s = new String[random.nextInt(6) + 1];
for (int j = 0; j < s.length; j++) {
s[j] = generateRandomChars("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
random.nextInt(10) + 3);
}
String X = join(" ", s);
assertEquals(C(X), PigLatin.pigIt(X));
}
}
}
import java.util.Arrays;
import java.util.stream.Collectors;
public class PigLatin {
public static String pigIt(String str) {
return Arrays.stream(str.split(" ")).map(PigLatin::pigify).collect(Collectors.joining(" "));
}
private static String pigify(String word) {
return word.length() > 1 || Character.isLetter(word.charAt(0)) ? word.substring(1) + word.charAt(0) + "ay" : word;
}
}
public class PigLatin {
public static String pigIt(String str) {
return str.replaceAll("(\\w)(\\w*)", "$2$1ay");
}
}
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.regex.Pattern;
public class PigLatin {
public static String pigIt(String str) {
return Arrays.stream(str.trim().split(" "))
.map(v -> v.matches("[a-zA-Z]+") ? v.substring(1).concat(v.substring(0, 1)).concat("ay") : v)
.collect(Collectors.joining(" "));
}
}
import java.util.regex.Pattern;
public class PigLatin {
private static final Pattern regex = Pattern.compile("(\\w)(\\w*)");
public static String pigIt(String str) {
return regex.matcher(str).replaceAll("$2$1ay");
}
}