Jump to content

Java-Modulos


AwesomeMcCoolName

Recommended Posts

Caeser Cipher code, however i need to make it loop around (i.e. if the shift is -3 and the letter is a, i need to make it go to x (88) rather than 62 (whatever char that is). I was told that i should use a modulo to do it, but not really sure how...
 

http://i.imgur.com/7MdJQH0.png <--code w/ formatting
 

public class CaesarCipher {
 
public static void main(String[] strings) {
int shift = 0;
String source = null;
String sourceCapital = null;
int n = 0;
int shiftedletter = 0;
int currentletter = 0;
 
while (shift == 0 || shift > 25 || shift < -25) {
System.out.println("Please enter the shift value (between -25..-1 and 1..25)");
shift = TextIO.getlnInt();
if (shift == 0 || shift > 25 || shift < -25)
System.out.println(shift + " is not a valid shift value.");
else
System.out.println("Using shift value of " + shift); }
System.out.println("Please enter the source text (empty line to quit)");
source = TextIO.getln();
if (source == null)
System.out.println("Bye.");
else
System.out.println("Source   :" + source);
n = source.length();
System.out.print("Processed:");
sourceCapital = source.toUpperCase();
while (currentletter < n) {
if (source.charAt(currentletter) == 32 || source.charAt(currentletter) == 46 || source.charAt(currentletter) == 59 )
shiftedletter = source.charAt(currentletter);
else
shiftedletter = sourceCapital.charAt(currentletter) + shift;
//convert back from int to char
System.out.print((char)shiftedletter);
currentletter++;}
}
 
}
Link to comment
Share on other sites

So basically you're asking us to do your CS101 homework for you

 

Anyways, this is a pretty easy one for me to explain in a few sentences

 

since you have the base ASCII character 'A' with the value of 65, and you have 26 letters, you can think of the letter 'Z' as being 65+25 (the first letter plus 25 letters.)  So, let's say you want to shift letters a,b,c,d to be f,g,h,i, AKA, a shift of +5.  You could do something along the lines of subtracting 65 from all of the ASCII values first (aka, 'A' = 0, 'B' = 1, etc.), and add five to them.  If you get something greater than 25, you can do a modulo 26 (the number you have % 26), and get what it would be, except wrapped around.  Then add 65 back to all of them again.

 

That's for doing a right shift.  Left shift is just as easy, but since this is obviously homework I'm gonna let you read the above and let you work it out on your own

Link to comment
Share on other sites

So basically you're asking us to do your CS101 homework for you

No, i already wrote the code, just not sure how to use a  modulos to loop it around from a to z rather than to some random char <65 or >91

Link to comment
Share on other sites

No, i already wrote the code, just not sure how to use a  modulos to loop it around from a to z rather than to some random char <65 or >91

 

So basically you're asking us to do your CS101 homework for you

 

Anyways, this is a pretty easy one for me to explain in a few sentences

 

since you have the base ASCII character 'A' with the value of 65, and you have 26 letters, you can think of the letter 'Z' as being 65+25 (the first letter plus 25 letters.)  So, let's say you want to shift letters a,b,c,d to be f,g,h,i, AKA, a shift of +5.  You could do something along the lines of subtracting 65 from all of the ASCII values first (aka, 'A' = 0, 'B' = 1, etc.), and add five to them.  If you get something greater than 25, you can do a modulo 26 (the number you have % 26), and get what it would be, except wrapped around.  Then add 65 back to all of them again.

 

That's for doing a right shift.  Left shift is just as easy, but since this is obviously homework I'm gonna let you read the above and let you work it out on your own

 

Edited that in, btw

 

the edit gives a simple overview

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...