CSCI-1320 Lab: Strings

Instructor: Todd Dole

Purpose Statement

This assignment gives practice manipulating Strings.

Instructions

Create a class called StringOperations, with the following methods: (You should also create a main method that calls each of these and prints the results to test them.)

  1. int wordCount(String anyString)
    Given a String parameter, count the number of words in the String and return the count as an int.
  2. boolean isPalindrome(String anyString)
    Given a String parameter, this method returns true if the String is a palindrome, and false if not. A palindrome is a String which is the same forward and back. (Hint: You will need to build a new String that is the reverse of the original String, and compare the two to see if they are the same.)
  3. String caesarCypher(String anyString, int shift)
    This method will perform a Caesar Cypher and returns a String with the encrypted text. A caesar cypher simply shifts each letter a-z forward by the shift amount. If shift is set to 1, then a becomes b, b becomes c, etc. If shift is set to 2, a becomes c, b becomes d, c becomes e. And so on. Letters "wrap around" so that z shifted one spot becomes a. Non-letter characters (spaces, punctuation, etc) are not changed.

    Hint: you will need to build your new String one character at a time. For each character, check if it is a letter before shifting.

    char variables each have a numeric value. For instance, 'a' is == to 97. You can use addition with char, so for example 'a' + 1 == 'b'. You can also use subtraction for the wrap around, or some clever modulo code.

    You will need to handle lower case (a-z) and upper case(A-Z) separately.

    Here is some helpful code you can experiment with to better understand how this works:

                        char myChar = 'a';
                        int myInt = myChar; // Gets the numeric value of myChar and places it into myInt
                        System.out.printf("%d\n",myInt);  // prints myInt

  4. Bonus: If you write your code so that it can handle a negative value for shift, you can use your method to decypher as well! (You will need to handle wrapping both directions.).

Example Output from main method

                    "The quick brown fox jumps over the lazy dog." contains 9 words.
                    "tacocat" is a palindrome!
                    "Hello, World" is not a palindrome.
                    "Hello, World!" shifted 2 characters is "Jgnnq, Yqtnf!"
                    "Jgnnq, Yqtnf!" shifted -2 characters is "Hello, World!"
            
Submit your working .java file through Canvas.