# CS173: Intro to Computer Science - Strings Revisited (3 Points)

Developed by Professor Tralie and Professor Mongan.

# Exercise Goals

The goals of this exercise are:
1. To iterate over String variables and return part-way through the string iteration.
Implement the program below that converts a String to "Pig Latin" according to the rules at the bottom of the page.

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
Clicking Run below will check your work and, if it passes, will submit your work automatically. You must be connected to the VPN for submission to be successful! You will receive a copy of your code via e-mail, so you'll know that it was submitted if you receive that e-mail! VPN access requires Multi-Factor Authentication, which sends you a code when you log into the network. Instructions on configuring these for your account can be found here.

### 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);

## Rules

We will simplify the rules as follows:

• If a String starts with a vowel, simply append “yay” to the end of the String.
• 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 the String. Then, append “ay” to the end of the resulting String.

## Hints

Given a String variable (let’s say it’s called str):

• You can use the str.substring(i) method to return a String containing all characters in str from position i to the end of the String. Don’t forget that String indices are numbered starting from 0.
• You can use the str.substring(i, j) method to return a String containing all characters in str from position i up to (but not including) position j of the String.
• You can use the + operator to concatenate two String values (or their substrings, which are also just String values!).