Returns, String practice

posted by: Ms. Martin 9 November 2010 No Comment
  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. Write a method countChar that takes two parameters: a string and a character and returns how many times the character is in the String.  countChar(“Hello, world!”, ‘o’) should return 2.  Notice the single quotes around ‘o.’ (this uses a cumulative sum pattern)
  3. Write a method that given a string returns a string made of repetitions of that string.  For example, a call on repeat(“Tacos”, 5) should return “TacosTacosTacosTacosTacos”
  4. Write a method that given a string with at least two dashes returns the text between the first two.  For example, the call middleText(“You may -absolutely- not go to the bathroom”) should return “absolutely.”
  5. Write a method named isAllVowels that returns whether a String consists entirely of vowels (a, e, i, o, or u, case-insensitively). If every character of the String is a vowel, your method should return true. If any character of the String is a non-vowel, your method should return false. Your method should return true if passed the empty string, since it does not contain any non-vowel characters. For example, here are some calls to your method and their expected results:
    Call Value Returned
    isAllVowels(“eIEiO”) true
    isAllVowels(“oink”) false
  6. 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:
    Your secret message: Brad thinks Angelina is cute
    Your secret key: 3
    The encoded message: eudg wklqnv dqjholqd lv fxwh

Impress me (optional)

  1. 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.
  2. 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
  3. 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 (19 votes, average: 2.95 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>