Free University of Bolzano/Bozen
Faculty of Computer Science - Bachelor in Applied Computer Science
Bachelor in Production Engineering
Introduction to Programming - A.A. 2004/2005

Exercises on recursion (strings)


  1. Write a static method
    public static boolean appearsIn(String s, char c)
    that, given a string s and a character c, verifies whether c appears in s.
    For example, the call appearsIn("pippo", 'i') should return true.

    Solution

  2. Write a static method
    public static int occurrences(String s, char c)
    that, given a string s and a character c, returns the number of occurrences of c in s.
    For example, the call occurrences("pippo",'p') should return 3.

    Solution

  3. Write a static method
    public static String underscore(String s)
    that, given a string s, returns a string obtained from s by replacing each blank with the underscore character ('_').
    For example the call underscore("pippo and topolino") should return the string "pippo_and_topolino".

    Solution

  4. Write a static method
    public static String reverse(String s)
    that, given a string s, returns the string constituted by the characters of s in inverse order.
    For example the call reverse("pippo") should return the string "oppip".

    Solution

  5. Write a static method
    public static String removeVocals(String s)
    that, given a string s, returns a string obtained from s by removing all vocals.
    For example the call removeVocals("pippo") should return the string "ppp".

    Solution

  6. Write a static method
    public static boolean palindrome(String s)
    that, given a string s, returns the boolean value true if the string is a palindrome, false otherwise.
    A string is palindrome if, when the string spelled from left to right it is equal to the string spelled from right to left. (e.g., "anna" and "ailatiditalia" are palidrome strings).

    Solution

  7. Write a static method
    public static void permutations(String s, PrintStream p)
    that, given a string s and a PrintStrem p, prints on p all strings obtained by permuting the characters of s.
    For example, the call permutations("abc", System.out) should print the following on standard output:
    abc
    acb
    bac
    bca
    cab
    cba
    

    Solution

Class Main to test the methods above.


Sunday, 9-Jan-2005 13:07:52 CET