CS219 Exam 1 Practice Questions

  1. Write a function palindrome that returns true if a string passed to it is a palindrome.
          if (palindrome("madam")) {
              System.out.println("madam is a palindrome");      
          }
          
  2. Make your palindrom function robust by having it work in the presence of spaces, punctuation, and case.
          String str = "Madam, I\'m Adam.";
          if (palindrome(str)) {
              System.out.println(str + " is a palindrome");      
          }
          
  3. Pretend that Java does not have any builtin functions to convert a lower case character to an upper case character. Write a function toUpper that takes a character as a parameter and converts it to upper case. If the character is not a lower case character it just returns it unmodified. For exmaple the test code below
            System.out.print(toUpper('a'));
            System.out.print(toUpper('q'));
            System.out.print(toUpper('B'));
            System.out.println(toUpper(';'));
          
    should print the characters AQB;
  4. Should the toUpper method you wrote above be declared static? Explain your answer.
  5. Write a program that reads a file a counts the number of times each alphabetic character appears in the file. The program should be case insensitive. For example is we had the text How many times does each character in this sentence occur? Your program should report back that
            a occurred 4 times
            b occurred 0 times
            c occurred 5 times
            and so on
         
    Hint: Use an array of length 26 and let index 0 correspond to the number of times 'a' occurred, index 1 for 'b', and so on.