LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2003, 12:45 PM   #1
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Rep: Reputation: 16
Thumbs down Caesar Cypher algorithm (JAVA)


This code is supposed to "encrypt" all characters of the sentence you input by shifting it 4 characters down the alphabet.

For example, if I type "angel" it should display "erkip".


But I'm stuck. My code just prints the first "encrypted" character of the sentence. Like if I type "angel" it just prints "e".

I tried everything...like modifying the (char)iteration (last line), variable by adding 4 to it and I get one of those "out of range errors".
In fact, any modification to any other variable gives me the "out of range" errors.

I was wondering if maybe you guys could enlighten me with your wisdom. I think the problem is in the for-loop but I can't figure out exactly where.

I'll post the final code when (if) I get it done. It might be useful to somebody else.

Thanks,

randomX



Code:
public class CaesarCypher
{
	public static final int MOVE_DOWN = 4;
	public static void main(String [] args)
	{

		String plainText;
		int iteration;
		char character;

		System.out.println("Enter a sentence: ");
		plainText = <some custom made method to get input from keyboard>;


		for (iteration = 0; iteration < plainText.length(); iteration++)
		{
			character = plainText.charAt(iteration);  //get characters
			iteration = (char) (character + MOVE_DOWN); //perform shift
			System.out.println("Encrypted sentence is: " + (char)iteration);
		}


	}
}

Last edited by randomx; 10-05-2003 at 09:59 PM.
 
Old 10-05-2003, 02:10 PM   #2
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Rep: Reputation: 32
I notice you are assigning the result of (char) (character + MOVE_DOWN) to the same variable you use to control your cycle.

Are you sure that's ok? In my opinion, that is the cause why your iteration stops early.

By the way, with this:

Quote:


for (iteration = 0; iteration < plainText.length(); iteration++)
{
character = plainText.charAt(iteration); //get characters
iteration = (char) (character + MOVE_DOWN); //perform shift
System.out.println("Encrypted sentence is: " + (char)iteration);
}


}
}
You will be getting something like:

Encrypted sentence is a
Encrypted sentence is b
Encrypted sentence is c
Encrypted sentence is d


You should try something like:
Code:
 

System.out.print("Encrypted sentence is: " );

for (iteration = 0; iteration < plainText.length(); iteration++)
{
      character = plainText.charAt(iteration); //get characters
      character = (char) (character + MOVE_DOWN); //perform shift
      System.out.print(character);
}

System.out.println();
Now I've only been using Java for like 2 weeks so I'm not sure if I'm correct here








EDIT

Now this is the code I used and its output:

Code:
 
import java.io.*; 


public class CaesarCypher 
{
	public static void main(String args[])
	{
		final int MOVE_DOWN = 4; 
		String plainText = "angel"; 
		char character; 

		System.out.print("Encrypted sentence is: " );

		for (int iteration = 0; iteration < plainText.length(); iteration++)
		{
      			character = plainText.charAt(iteration); //get characters
      			character = (char) (character + MOVE_DOWN); //perform shift
      			System.out.print(character);
		}

		System.out.println(); 
		

	}
}

OUTPUT:

Code:
 
[joseamuniz@localhost Java]$ javac CaesarCypher.java
[joseamuniz@localhost Java]$ java CaesarCypher
Encrypted sentence is: erkip

Last edited by Jose Muņiz; 10-05-2003 at 02:26 PM.
 
Old 10-05-2003, 08:03 PM   #3
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
Thumbs up correct my first algorithm but need avoid spaces

right on!! Thanks man. Thanks to your tips I was able to get it going. Not only that, but it made me realize that my former algorithm was not quite right. Like on the "w" is pointing to "}". It should point to "a".

I modified the "formula", the shift algorithm. I noticed that former formula was not quite right.

by the way...How do I leave spaces in the encrypted sentences instead of putting a character?

anyway...let me show you the NEW for-loop and NEW formula.

Code:
for (int iteration = 0; iteration < plainText.length(); iteration++)
		{
      			character = plainText.charAt(iteration); //get characters
      			character = (char) ('a' + (character - 'a' + MOVE_DOWN) % 26); //perform shift. New formula.

      			System.out.print(character);
		}

		System.out.println(); 

}//closes void main
} //closes class

Now, How do I leave spaces in the encrypted sentences instead of putting a character?

Right now if you input a string like "angels team" , the space gets printed out as an X. A big "x".

It says "erkipwXxieq"
erkip=angel team=xieq space=X

it should say simply "erkip xieq"

How can I keep the spaces?


thanks bro.

Last edited by randomx; 10-05-2003 at 10:00 PM.
 
Old 10-05-2003, 09:24 PM   #4
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Re: correct my first algorithm but need avoid spaces

Quote:
Originally posted by randomx
Now, How do I leave spaces in the encrypted sentences instead of putting a character?
Check the character before shifting it to make sure it isn't a space. If it is, skip it and move on to the next character:

Code:
for ( int iteration = 0 ; iteration < plainText.length() ; iteration++ ) {

   character = plainText.charAt( iteration );

   if ( character != ' ' ) {
      character = (char) ( 'a' + ( character - 'a' + MOVE_DOWN ) %26 );
   }

   System.out.print(character);
}
Also, when you post messages you can wrap your code in [ CODE] and [ /CODE] tags so that they keep their formatting (and you don't have to add a comment saying "code starts here"). See how nice my posted code looks? You can even go back and edit your previous posts to make your code look good... hint hint.
 
Old 10-05-2003, 10:16 PM   #5
randomx
Member
 
Registered: Feb 2003
Location: Hawaii
Distribution: Debian
Posts: 130

Original Poster
Rep: Reputation: 16
thanks bro. got it.

thank you so much bro.

I got it working now.

randomX

PS: your tips on formatting code on my postings are great.

 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Tutorial for writing cypher kernel module ta0kira Programming 7 06-19-2011 10:43 PM
Do you memorize the algorithm?For... shakedown1987 Programming 5 08-05-2004 07:21 AM
skiplist algorithm Tafta Programming 1 12-20-2003 01:43 PM
NAGLE Algorithm saintt Linux - Networking 1 12-03-2001 07:48 AM
assistance with an algorithm mandrake_linux Programming 3 07-27-2001 04:03 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration