Skip to content

Instantly share code, notes, and snippets.

@deepakkhattar26o2
Created January 13, 2022 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepakkhattar26o2/f238dac1058b5aaf39b31d8f4e27073d to your computer and use it in GitHub Desktop.
Save deepakkhattar26o2/f238dac1058b5aaf39b31d8f4e27073d to your computer and use it in GitHub Desktop.
A Java Program that ciphers a message by shifting its letters by some number
import java.util.Scanner;
public class cipher {
private static char shiftLower(char c, int shift ){
return (char)(((int)c -97 + shift)%26 + 97);
}
private static char shiftUpper(char c, int shift ){
return (char)(((int)c -65 + shift)%26 + 65);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int shift = 7;
System.out.print("Enter A Message: ");
String message = sc.nextLine();
String enc="";
for(int i=0;i<message.length();i++) {
if(message.charAt(i)>='a' && message.charAt(i)<='z'){
enc+=shiftLower(message.charAt(i), shift);
}
else if(message.charAt(i)>='A' && message.charAt(i)<='Z'){
enc+=shiftUpper(message.charAt(i), shift);
}
else{
enc+=message.charAt(i);
}
}
System.out.println("Ciphered Message: " + enc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment