Prelab 6: Manipulating Strings

String manipulation refers to the use of methods to manipulate, alter and determine properties of strings.

Readings

The following items, which you should read prior to lab, contain information on how to manipulate strings using Java.

Pig Latin

In Lab 6, you will learn how much fun manipulating strings can be. One thing you need to do in preparation for this lab is to understand the rules of Pig Latin.

Pig Latin Exercises

Now that you know the rules of the Pig Latin world, translate the following paragraph into Pig Latin:

Every monkey would like to be
In my place instead of me
Cause I am the king of bongo, baby
I am the king of bongo bang

          (Source: An excerpt of the lyrics from "Bongo Bang" by Manu Chao.)

String Manipulation in Java

The Java String class contains methods for string manipulation. Here are some common examples:

String Manipulation Exercises

Given the variable declarations

String s = "Bongo bang is a weird song!?!";
String t = "I don't want to be a monkey";
String answer;
int ans;
answer the following questions:
  1. answer = t.substring(21,24);

    What is answer?

  2. ans = s.length();

    What is ans?

  3. ans = t.indexOf("t");

    What is ans?

  4. answer = t.substring(8,10) + s.substring(3,5);

    What is answer?

  5. ans = t.length() - s.indexOf(w);

    What is ans?

Writing a Pig Latin function

You will be using a subroutine called findFirstVowel that has already been written for you. The subroutine findFirstVowel takes a String as its argument and returns an int that represents the location of the first vowel. You use this integer to determine how to convert the string to Pig Latin.

The code for findFirstVowel and its auxillary function isVowel is below: public boolean isVowel(char c) { if (c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c == 'u' || c == 'U') { return (true); } else { return (false); } } // findFirstVowel returns the index of the first occurrence // of a vowel in a word public int findFirstVowel(String word) { // declare and intiialize counter variable int i; i = 0; // loop through the characters in a word while (i < word.length()) { if (isVowel(word.charAt(i))) return i; i = i + 1; } return (-1); }
Example: findFirstVowel("Spartacus") returns 2.

At this point, you need not worry about the how isVowel works.

Now, you need to write a function that takes a word (string s) and returns a string that is the Pig Latin version of the word.

// Subroutine ConvertToPigLatin public String ConvertToPigLatin(String s) { String result = ""; // Case 1: the word begins with a vowel // This case has been done for you already if (findFirstVowel(s) == 0 ) { result = (s + "way"); } // Case 2: the word doesn't begin with a vowel // CHANGE test (true) to match words that do not begin with a vowel else if (true ) { // # FILL IN - set result to what? } // Case 3: the word doesn't have any vowels // CHANGE test (true) to match words that do not have any vowels else { // # FILL IN - set result to what? } return result; }
Example: ConvertToPigLatin("apple") returns "appleway".

Notice the different indications for the various cases in the ConvertToPigLatin subroutine. For each case you may need to add an if-else statement to test if the case is true. Within each if statement you will design an algorithm to convert the word to Pig Latin. Note that the first case (when a word starts with a vowel) has already been done for you. As always, it might not be a bad idea to come up with the algorithm in "psuedocode" first and then translate it to Java.

Finish the ConvertToPigLatin subroutine either on paper or in Notepad or some other editor and bring that with you to lab.



Jeff Forbes <forbes@cs.duke.edu>
Last modified: Fri Feb 14 10:21:21 EST 2003