Strings and Casting Tutorial – Coding a Cesar Cipher in Java

I decided to do a mini tutorial on strings and how casting works in Java.

In a nutshell strings are an array of characters. The following snippet is a common way of coding a string.

String str = "asdf";

So if a string is an array, what’s an array?
I view arrays as a collection of memory address. Let’s code the previous string as array of characters.

char[] arr = new int[4];
arr[0] = 'a';
arr[1] = 's';
arr[2] = 'd';
arr[3] = 'f';

In the first line of the snippet we created an array with a limit of four characters. As you can see the first index of an array is 0 and ends with 3.
You can also create an character array from a string object by doing something like this:

char[] arr = str.toCharArray();

Now that we are starting to see what a is string, lets take a look at a character. A character is really a number. Java strings are based off of ASCII values, so the character ‘A’ is really the number 65. You can check the ASCII value of a character by casting the character as an integer. Casting is were you convert one datatype into another datatype and here is how to you can do it.

int ascii = (int) 'A';

So are characters integers, nope they’re bytes but don’t worry you can still work with them using denary numbers. The next snippet creates a character from an integer number.

char ch = (char) 65;

To summarized, strings are an array of characters. Characters are integers and integers are bytes. To demonstrate a couple of the things you can do with a string I wrote a Cesar cipher using Java. A Cesar cipher is a basic cipher that rotates characters using a given index. If we have a character ‘A’ and the cipher advances three characters, the encoded character for ‘A’ would then be ‘D’. To decode this character when would then have to rotate backwards three character to get the original character ‘A’.

My cipher only has one method called rotate. You can encode and decode strings by doing something similar to the following snippet.

Cesar c = new Cesar();
String encodedStr = c.rotate("asdf", 1);
String decodedStr = c.rotate(encodedStr, -1);

Anyway here is my source code for the cipher. You might need to wear your coding goggles to follow this one. On a side note, I used the StringBuilder datatype. StringBuilder works similar to a list and you can chuck what ever you want into it to create a string.

public class Cesar {
	public Cesar(){}  //Default constructor
	public String rotate(String str, int swift){
		StringBuilder rotatedStr = new StringBuilder();
		for(char ch : str.toCharArray()){
			//Checking for uppercase letters
			if(ch >= 65 && ch <= 90 ){
				int uncheckChar = ch + swift;
				if( uncheckChar > 90 )
					rotatedStr.append( (char) ( 64 + ( uncheckChar - 90 ) )  );
				else if( uncheckChar < 65 )
					rotatedStr.append( (char) ( 91 - ( 65 - uncheckChar ) ) );
					rotatedStr.append( (char) uncheckChar );
			//Checking for lowercase letters
			else if( ch >= 97 && ch <= 122 ){
				int uncheckChar = ch + swift;
				if( uncheckChar > 122 )
					rotatedStr.append( (char) ( 96 + ( uncheckChar - 122 ) ) );
				else if ( uncheckChar < 97 )
					rotatedStr.append( (char) ( 123 - ( 97 - uncheckChar ) ) );
					rotatedStr.append( (char) uncheckChar );
			//Non-Alphabetic ASCII character are left alone.
		return rotatedStr.toString();

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s