Strings Practice

posted by: Mr. Bergquist 20 November 2011 No Comment

In case you need them: Strings! Presentation and our Handout: Strings .

  1. Write a method that prompts the user for a word and prints out its equivalent in Pig Latin. To translate a word to Pig Latin, take the initial letter, move it to the end of the word and add ‘ay’. The new suffix (first letter with ay) should be printed in all caps. Here is a sample run of the program:
    Give me a word to translate to Pig Latin: trash
    Your word is rashTAY in Pig Latin!

    Challenge: can you translate a full sentence from the user? Separating words will take some cleverness.
  2. Self-Check 3.10: printStrings
  3. Exercise 3.17: padString
  4. Exercise 3.18: vertical
  5. A Caesar cipher is a simple encryption scheme in which a message is encoded by shifting each letter by a given amount. For example, with a shift of 3, A goes to D, H goes to K, X goes to A and Z goes to C.  Write a method that reads a message from the user and performs a Caesar cipher on its letters as follows (Note: that blank spaces are maintained and not encoded) – And please test your code with a secret key of 14 before asking for sign off:
    Your secret message: Brad thinks Angelina is cute
    Your secret key: 3
    The encoded message: eudg wklqnv dqjholqd lv fxwh

Impress me (optional)

  1. Exercise 4.18: wordCount - Extra now (it’s easier done with a while loop)
  2. Given a string, does “xyz” appear in the middle of the string? To define middle, we’ll say that the number of chars to the left and right of the “xyz” must differ by at most one. This problem is harder than it looks.
    xyzMiddle(“AAxyzBB”) → true
    xyzMiddle(“AxyzBB”) → true
    xyzMiddle(“AxyzBBB”) → false 13.
  3. Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note: Character.isDigit(char) tests if a char is one of the chars ’0′, ’1′, .. ’9′. Integer.parseInt(string) converts a string to an int.)
    sumNumbers(“abc123xyz”) → 123
    sumNumbers(“aa11b33″) → 44
    sumNumbers(“7 11″) → 18
  4. An emirp is a prime number that is also prime when its digits are reversed, and that is not also a palindrome. For instance, 13 is an emirp because its reversal, 31, is also prime; 23 is not an emirp, even though it is prime, because its reversal, 32, is not prime; and 101 is not an emirp, even though it is prime, because it is a palindrome.Your task is to enumerate the emirps below a million
1 Star2 Stars3 Stars4 Stars5 Stars (10 votes, average: 3.60 out of 5)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>