Ceasar cipher java code

Ceasar cipher java code



I have created a code for a caesar cipher with the shift being 25. My code is not encoding the first half of the alphabet, but it is encoding the second half.



I don't know why?



For example, if I try to encode "abcdefghijklmnopqrstuvwxyz", it comes out as "abcdefghijklmmlkjihgfedcba". Only the second half of the alphabet is encoded.



Here is the code:


import java.util.Scanner;
public class Project1

public static void main(String args)

//creating alphabet and coded alphabet arrays
char alphabet = 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ';
char codedAlphabet = 'z','y','x','w','v','u','t','s','r','q','p','o','n','m','l','k','j','i','h','g','f','e','d','c','b','a',' ';

//prompting user for encoding or decoding, and for the message
Scanner scan = new Scanner(System.in);
System.out.println("Enter the message: ");
String s = scan.nextLine();
s.toLowerCase();
StringBuffer message = new StringBuffer( s );

Scanner input = new Scanner(System.in);
System.out.println("Enter 1 to encode message, 2 to decode message: ");
int choice = input.nextInt();
if(choice == 1)
encode(message, alphabet, codedAlphabet);
System.out.println(message);
else if(choice == 2)




//method for encoding
public static void encode(StringBuffer message, char a, char b)
for(int i = 0; i<message.length(); i++)
for(int j = 0; j<a.length; j++)
if( message.charAt(i) == a[j])
message.setCharAt(i, b[j]);










add break; after message.setCharAt(i, b[j]);
– Ilya Bursov
Aug 17 at 17:43


break;


message.setCharAt(i, b[j]);





Btw, your s.toLowercase(); line doesn't do what you maybe think it does.
– Andy Turner
Aug 17 at 17:45


s.toLowercase();





And the coded alphabet is not a Caesar cipher of alphabet, since it is basically just reversed.
– Andy Turner
Aug 17 at 17:47






I think this is an example but is a Caesar cipher
– Andrea Bellizzi
Aug 17 at 17:50





@AndyTurner What does the lowercase method do? I thought it sets all of the characters to lowercase
– Shashank Saini
Aug 17 at 17:50




2 Answers
2



Your check for finding the right character is correct. But when you replace the character you have to break that inner loop. Otherwise you keep replacing the same character position more than once if the selected encryption pairing fits. In this case you replace your first character a to z, which is correct, but then replace this new character z again (back to a), which is wrong.


a


z


z


a


public static void encode(StringBuffer message, char a, char b)
for(int i = 0; i<message.length(); i++)
for(int j = 0; j<a.length; j++)
if( message.charAt(i) == a[j])
message.setCharAt(i, b[j]);
// we found the character and replaced it, we
// are done with this position, get to the next "i"
break;






According to your question code, I think this will be the answer.


public static void encode(StringBuffer message, char a, char b)
for(int i = 0; i<message.length(); i++)
for(int j = 0; j<a.length; j++)
if( message.charAt(i) == a[j])
message.setCharAt(i, b[j]);
break;





public static void decode(StringBuffer message, char a, char b)
for(int i = 0; i<message.length(); i++)
for(int j = 0; j<a.length; j++)
if( message.charAt(i) == b[j])
message.setCharAt(i, a[j]);
break;









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj