Reputation: 17
I'm trying to write a program that checks a user supploed sentence and converts it to Pig Latin. I'm trying to have the program check to see if the first letter is a vowel or not and return that using a boolean expression. Next I'm trying to get the program to cut off the first letter from the word and add it to the end of the word. Finally its supposed to add way
if it is a vowel and ay
if it not a vowel. Any help or advice will be greatly appreciated.
public class PigLatin {
public static void main(String[] argv) {
if (argv.length > 0) {
for (int i = 0; i < argv.length; i++) {
char firstLetter = aStringVariable.charAt(0);
}
}
public static boolean isVowel(char c) {
char[] vowels = new char[] {'a', 'e', 'i', 'o', 'u', 'y'};
for(int i = 0; i < vowels.length; i++) {
if(Character.toString(vowels[i]).equalsIgnoreCase(Character.toString(c))) {
return true;
}
}
return false;
}
public static String makePigLatin(boolean vowel, String word)
{
String everythingButTheFirstLetter = word.substring(1);
String n = everythingButTheFirstLetter + firstLetter;
if (true)
{
System.out.println(n + "way");
}
if (false)
{
System.out.println(n + "ay");
}
}
}
Upvotes: 0
Views: 5625
Reputation: 21
You can also try something like:
public static string PigLatin(string sentence)
{
const string vowels = "AEIOUaeiou";
sentence = sentence.ToLower();
var returnSentence = "";
foreach (var word in sentence.Split())
{
var firstLetter = word.Substring(0, 1);
var restOfWord = word.Substring(1, word.Length - 1);
var currentLetter = vowels.IndexOf(firstLetter, StringComparison.Ordinal);
if (currentLetter == -1)
{
returnSentence += restOfWord + firstLetter + "ay ";
}
else
{
returnSentence += word + "way ";
}
}
returnSentence = returnSentence.TrimEnd();
return returnSentence;
}
The output for the sentence "The Quick Brown Fox Jumped Over The Lazy Dog" will be :
hetay uickqay rownbay oxfay umpedjay veroay hetay azylay ogday
Upvotes: 1
Reputation: 201439
Try something like this -
public static class PigLatin {
public static boolean isVowel(char c) {
switch (Character.toLowerCase(c)) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
}
return false;
}
public static String makePigLatin(String word) {
char firstLetter = word.charAt(0);
String everythingElse = word.substring(1);
String n = everythingElse + firstLetter;
if (isVowel(firstLetter)) {
return n + "way";
}
return n + "ay";
}
}
public static void main(String args[]) {
String[] words = { "fair", "away", "test" };
for (String word : words) {
String s = PigLatin.makePigLatin(word);
System.out.println(word + " is " + s);
}
}
Output is
fair is airfay
away is wayaway
test is esttay
Upvotes: 2