-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d9b3eb8
commit 40a6e2e
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
src/main/java/com/levelup/java/exercises/beginner/PigLatin.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.levelup.java.exercises.beginner; | ||
|
||
import java.util.List; | ||
import java.util.Scanner; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.common.base.Splitter; | ||
import com.google.common.collect.Lists; | ||
|
||
/** | ||
* This java example will demonstrate how to convert a phrase to pig latin. | ||
* | ||
* @author Justin Musgrove | ||
* @see <a href='http://www.leveluplunch.com/java/exercises/pig-latin-translator/'>Pig latin translator</a> | ||
*/ | ||
public class PigLatin { | ||
|
||
public static void main(String[] args) { | ||
// Create a Scanner object for keyboard input. | ||
Scanner keyboard = new Scanner(System.in); | ||
|
||
// Ask user to enter a string to translate | ||
System.out.println("Enter a string to translate to Pig Latin."); | ||
System.out.print("> "); | ||
String input = keyboard.nextLine(); | ||
|
||
// close the keyboard | ||
keyboard.close(); | ||
|
||
List<String> elementsInString = Lists.newArrayList(Splitter.on(" ") | ||
.split(input)); | ||
|
||
Function<String, String> swapFirstLastChar = new Function<String, String>() { | ||
@Override | ||
public String apply(String s) { | ||
if (s.length() <= 1) { | ||
return s; | ||
} else { | ||
return s.substring(1, s.length()) + s.charAt(0); | ||
} | ||
} | ||
}; | ||
|
||
List<String> translatedString = elementsInString.stream() | ||
.map(String::trim).map(String::toUpperCase) | ||
.map(swapFirstLastChar).map((String s) -> { | ||
return s + "AY"; | ||
}).collect(Collectors.toList()); | ||
|
||
|
||
System.out.println("Translated sentence is: " | ||
+ String.join(" ", translatedString)); | ||
} | ||
} |