8 – Strings and Characters

Sample Code

 String, char, int

public class StrAsc
{
    public static void main (String args [])
    {
        String s = "A String";
        System.out.println("The character at index 2 is " + s.charAt(2));
        System.out.println("The ASCII equivalent is " + (int)s.charAt(2));
    }
}

Exercises

  1. Convert the following string to its ASCII values: “I never saw a purple cow”
  2. If a = 1, b=2, c=3… convert the following String to its equivalent character codes: “DailyJava”
  3. ROT13 (“rotate by 13 places”, sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher, developed in ancient Rome. Write a program that will accept a String as input then output that string under a ROT13 transformation, so input of HELLO will result in output of URYYB
  4. Write a ROT-N cipher, similar to a ROT13 cipher, where a string and a shift are input, and a string is outputted with the characters shifted by N, so if the input is “DAD” and 1, the output is “EBE”
  5. Write a program that uses ASCII values to convert lowercase characters to uppercase, so input of “this” will result in output of “THIS”. DO NOT use library methods such as toUpperCase()
  6. There are 62 Alphanumeric Characters: [A-Za-z0-9]. Any other character, such as %,(): is non-alphanumeric. There are also a number of control or non-printing characters. These include Line Feed, Carriage Return and Tab. Write a program that imports a text file and prints the number of alphanumeric characters it contains.
  7. Write a program that accepts a string cipher as an input and ouputs a string plaintext containing every second letter from input. Test your program using the input “Knives” and “Forks”. You should get the output “nvs” and “ok” respectively

Leave a Comment