# Help in code understanding!

Page 1 of 1

## 6 Replies - 1493 Views - Last Post: 17 November 2010 - 09:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=200228&amp;s=d617cf0467dc3bbd661ae7c3fbd678c2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ROCKY86

Reputation: 0
• Posts: 47
• Joined: 05-August 07

# Help in code understanding!

Posted 15 November 2010 - 10:04 AM

hey this need help to understand what is this code doing! bascially need to create an 2 dimenensional array with size 26 by 26 cause from A to Z
```public class Ciphers
{
private char[][] lookupTable = new char[26][26];
private String keyword;
public Ciphers()
{
lookupTable[0][0] ='A';
int row, col, i;
for(row=0; row<26; row++)
{
lookupTable[row][0]= (char)((int)lookupTable[0][0]+ row); 		}

for(col=0; col<26; col++)
{
lookupTable[0][col]= (char)((int)lookupTable[0][0]+ col); 	}

for(col=1; col<26; col++)
{
for(row=1; row<26; row++)
{
int tot = row + col;
if (tot >= 26)
{
tot -= 26;//when exceeds 25 that is 'z', then minus no. of characters
}
lookupTable[row][col] = Character.toUpperCase ((char)(((int)'a')+tot));
}
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Help in code understanding!

### #2 EdwinNameless

Reputation: 128
• Posts: 723
• Joined: 15-October 09

## Re: Help in code understanding!

Posted 15 November 2010 - 10:23 AM

It gives all the rotations of the alphabet, handy to cipher into ROT-x, with x chosen.

### #3 baavgai

• Dreaming Coder

Reputation: 7505
• Posts: 15,553
• Joined: 16-October 07

## Re: Help in code understanding!

Posted 15 November 2010 - 10:30 AM

Well, I can see why you might be confused. This wins my worse formatted code a of the month award. It's also awfully sloppy and pedantic.

It's doing this:

```public class Ciphers {
private static final int SIZE = 26;
private char[][] lookupTable = new char[SIZE][SIZE];

public Ciphers() {
char beginValue = 'A';
char endValue = 'Z';
for(int row=0; row<SIZE; row++) {
char value = (char)(row + beginValue); // row=0, value='A'.  row=1, value='B'
for(int col=0; col<SIZE; col++) {
if (value>endValue) { value=beginValue; }
lookupTable[row][col] = value;
value++;
}
}
}

public void print() {
for(int row=0; row<SIZE; row++) {
for(int col=0; col<SIZE; col++) {
System.out.print(lookupTable[row][col]);
}
System.out.println();
}
}

public static void main (String[] args) {
new Ciphers().print();
}
}

```

Results:
```ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
CDEFGHIJKLMNOPQRSTUVWXYZAB
DEFGHIJKLMNOPQRSTUVWXYZABC
EFGHIJKLMNOPQRSTUVWXYZABCD
FGHIJKLMNOPQRSTUVWXYZABCDE
GHIJKLMNOPQRSTUVWXYZABCDEF
HIJKLMNOPQRSTUVWXYZABCDEFG
IJKLMNOPQRSTUVWXYZABCDEFGH
JKLMNOPQRSTUVWXYZABCDEFGHI
KLMNOPQRSTUVWXYZABCDEFGHIJ
LMNOPQRSTUVWXYZABCDEFGHIJK
MNOPQRSTUVWXYZABCDEFGHIJKL
NOPQRSTUVWXYZABCDEFGHIJKLM
OPQRSTUVWXYZABCDEFGHIJKLMN
PQRSTUVWXYZABCDEFGHIJKLMNO
QRSTUVWXYZABCDEFGHIJKLMNOP
RSTUVWXYZABCDEFGHIJKLMNOPQ
STUVWXYZABCDEFGHIJKLMNOPQR
TUVWXYZABCDEFGHIJKLMNOPQRS
UVWXYZABCDEFGHIJKLMNOPQRST
VWXYZABCDEFGHIJKLMNOPQRSTU
WXYZABCDEFGHIJKLMNOPQRSTUV
XYZABCDEFGHIJKLMNOPQRSTUVW
YZABCDEFGHIJKLMNOPQRSTUVWX
ZABCDEFGHIJKLMNOPQRSTUVWXY

```

The code should be reasonably clear. I don't know why the code you have got into case; really. The only trick to understand is that values A through Z follow each other as numeric values when stored. So, 'A'+2=='C'.

### #4 ROCKY86

Reputation: 0
• Posts: 47
• Joined: 05-August 07

## Re: Help in code understanding!

Posted 15 November 2010 - 04:43 PM

baavgai, on 15 November 2010 - 09:30 AM, said:

Well, I can see why you might be confused. This wins my worse formatted code a of the month award. It's also awfully sloppy and pedantic.

It's doing this:

```public class Ciphers {
private static final int SIZE = 26;
private char[][] lookupTable = new char[SIZE][SIZE];

public Ciphers() {
char beginValue = 'A';
char endValue = 'Z';
for(int row=0; row<SIZE; row++) {
char value = (char)(row + beginValue); // row=0, value='A'.  row=1, value='B'
for(int col=0; col<SIZE; col++) {
if (value>endValue) { value=beginValue; }
lookupTable[row][col] = value;
value++;
}
}
}

public void print() {
for(int row=0; row<SIZE; row++) {
for(int col=0; col<SIZE; col++) {
System.out.print(lookupTable[row][col]);
}
System.out.println();
}
}

public static void main (String[] args) {
new Ciphers().print();
}
}

```

Results:
```ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
CDEFGHIJKLMNOPQRSTUVWXYZAB
DEFGHIJKLMNOPQRSTUVWXYZABC
EFGHIJKLMNOPQRSTUVWXYZABCD
FGHIJKLMNOPQRSTUVWXYZABCDE
GHIJKLMNOPQRSTUVWXYZABCDEF
HIJKLMNOPQRSTUVWXYZABCDEFG
IJKLMNOPQRSTUVWXYZABCDEFGH
JKLMNOPQRSTUVWXYZABCDEFGHI
KLMNOPQRSTUVWXYZABCDEFGHIJ
LMNOPQRSTUVWXYZABCDEFGHIJK
MNOPQRSTUVWXYZABCDEFGHIJKL
NOPQRSTUVWXYZABCDEFGHIJKLM
OPQRSTUVWXYZABCDEFGHIJKLMN
PQRSTUVWXYZABCDEFGHIJKLMNO
QRSTUVWXYZABCDEFGHIJKLMNOP
RSTUVWXYZABCDEFGHIJKLMNOPQ
STUVWXYZABCDEFGHIJKLMNOPQR
TUVWXYZABCDEFGHIJKLMNOPQRS
UVWXYZABCDEFGHIJKLMNOPQRST
VWXYZABCDEFGHIJKLMNOPQRSTU
WXYZABCDEFGHIJKLMNOPQRSTUV
XYZABCDEFGHIJKLMNOPQRSTUVW
YZABCDEFGHIJKLMNOPQRSTUVWX
ZABCDEFGHIJKLMNOPQRSTUVWXY

```

The code should be reasonably clear. I don't know why the code you have got into case; really. The only trick to understand is that values A through Z follow each other as numeric values when stored. So, 'A'+2=='C'.

thank for your AWEOSME help!! now I got better understanding of the code!!

erm mind if i ask this part of the code??
```for(int col=0; col<SIZE; col++) {
if (value>endValue) { value=beginValue; }
lookupTable[row][col] = value;
value++;

```

this part of the code I understand only saying if the char is greater than Z than will reset to A back, but for the second part lookupTable[row][col] = value, I don't quite get it!! iszit it will keep printing until the value is greater than Z?

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: Help in code understanding!

Posted 15 November 2010 - 07:16 PM

ROCKY86, on 15 November 2010 - 05:43 PM, said:

thank for your AWEOSME help!! now I got better understanding of the code!!

erm mind if i ask this part of the code??
```for(int col=0; col<SIZE; col++) {
if (value>endValue) { value=beginValue; }
lookupTable[row][col] = value;
value++;

```

this part of the code I understand only saying if the char is greater than Z than will reset to A back, but for the second part lookupTable[row][col] = value, I don't quite get it!! iszit it will keep printing until the value is greater than Z?

not to Z but to what is left
if the column start at K you will have
KLMNOPQRSTUVWXYZ
then value is reset to 'A' and you continue by doing value++;
ABCDEFGHIJ
till the end of the row

Looks like you are trying to do a Vigenere square
This is my Secret Code second tutorial
http://www.dreaminco...igenere-square/

### #6 ROCKY86

Reputation: 0
• Posts: 47
• Joined: 05-August 07

## Re: Help in code understanding!

Posted 16 November 2010 - 08:18 AM

hi! thx for explaining! but my assignment is not doing a vigenere sq but similar! take in a user input to do a encript a plaintext letter:
1. plaintext is matched with a keyword letter
2. keyword letter determines the ROW of the matrix, find the row that begin with the keyword letter
3. The plaintext letter determines the COLUMN of the matrix
4. the ciphertext letter is found at the intersection of ROW and COLUMN

For example, if the keyword is "calculus" and the plaintext is "beammeupscotty";

The ciphertext will be "DELOGPOHUCZVNJ"

The first plaintext letter, 'b' is matched with the first keyword letter, 'c' so the first ciphertext letter 'D' is found in the matrix at row 'c' column 'b'. The second cipherext letter, 'E' is found at row 'a' column 'e' etc

SOUND CONFUSING right!!

right now i trying to understand a encryption method but do not know what it is doing!!

The first plain text letter, 'b', is matched with the first keyword letter
```public String encrypt(String plaintext)
{
String cipherTxt="";

int x, row, col, pt=0;
for(x=0; x<plaintext.length(); x++)
{
if(pt >= keyword.length())
pt = 0;
row = (int)(keyword.charAt(pt)-'a');
pt++;
col = (int)(plaintext.charAt(x) - 'a');
cipherTxt+=lookupTable[row][col];
}
return cipherTxt;
}

```

I only know this method take in a plaintext variable than do encryption! but it look so confusing!!

This post has been edited by ROCKY86: 16 November 2010 - 08:36 AM

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8381
• Posts: 31,956
• Joined: 06-March 08

## Re: Help in code understanding!

Posted 17 November 2010 - 09:15 PM

ROCKY86, on 16 November 2010 - 09:18 AM, said:

hi! thx for explaining! but my assignment is not doing a vigenere sq but similar! take in a user input to do a encript a plaintext letter:
1. plaintext is matched with a keyword letter
2. keyword letter determines the ROW of the matrix, find the row that begin with the keyword letter
3. The plaintext letter determines the COLUMN of the matrix
4. the ciphertext letter is found at the intersection of ROW and COLUMN
now this method take in a plaintext variable than do encryption! but it look so confusing!!

Sorry but THIS IS exactly what a Vigenere square is.
Go back to my tutorial or the preceding one on Ceasar cypher. You will see how from simple permutation we move to permutation based on a key. The key determining the number of shifts for each letter.