CS173: Intro to Computer Science - Strings Revisited (3 Points)
Developed by Professor Tralie and Professor Mongan.Exercise Goals
The goals of this exercise are:- To iterate over
String
variables and return part-way through the string iteration.
Enter your Ursinus netid before clicking run. This is not your ID number or your email. For example, my netid is wmongan
(non Ursinus students can simply enter their name to get this to run, but they won't get an e-mail record or any form of credit).
Netid |
PigLatin.java
public class PigLatin {
public static boolean isVowel(String input, int idx) {
boolean result = false;
if(input.toLowerCase().charAt(idx) == 'a' ||
input.toLowerCase().charAt(idx) == 'e' ||
input.toLowerCase().charAt(idx) == 'i' ||
input.toLowerCase().charAt(idx) == 'o' ||
input.toLowerCase().charAt(idx) == 'u') {
result = true;
} else {
result = false;
}
return result;
}
public static int firstVowelLocation(String input) {
/* TODO: fill this in. Return the index i corresponding to the first
position at which input.charAt(i) is a vowel */
}
public static String pigLatin(String input) {
String result;
int idx = firstVowelLocation(input);
/* TODO: fill this in according to the two possibilities given in
the rules at the bottom of the page. */
return result;
}
}
Driver.java
public class Driver {
public static void main(String[] args) {
System.out.print(PigLatin.pigLatin("trash"));
System.out.print("-");
System.out.print(PigLatin.pigLatin("pig"));
System.out.print("-");
System.out.print(PigLatin.pigLatin("smile"));
System.out.print("-");
System.out.print(PigLatin.pigLatin("egg"));
}
}
Driver.main(null);
Output
Rules
We will simplify the rules as follows:
- If a
String
starts with a vowel, simply append “yay” to the end of theString
. - Otherwise, modify the string so that all the letters of the
String
up to (but not including) the first vowel are moved to the end of theString
. Then, append “ay” to the end of the resultingString
.