Skip to content

Instantly share code, notes, and snippets.

@aadipoddar
Created December 18, 2021 17:01
Show Gist options
  • Save aadipoddar/279da3a8d73e15a0b897ebd7d9b88768 to your computer and use it in GitHub Desktop.
Save aadipoddar/279da3a8d73e15a0b897ebd7d9b88768 to your computer and use it in GitHub Desktop.
Encrypt Sentence with Caesar Cipher method
/*
*Enter a Sentence and Encrypt it with Caesar Cipher method (13 characters shift)
*Hello! How are you?
*Uryyb! Ubj ner lbh?
*/
import java.util.Scanner;
class Caesar_Cipher_Encryption
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sentence");
String sentence = sc.nextLine();
for(int i = 0; i < sentence.length(); i++)
{
char ch = sentence.charAt(i);
if(ch >= 'a' && ch <= 'z')
{
ch = (char)(ch + 13);
if(ch > 'z')
{
ch = (char)(ch - 26);
}
}
else if(ch >= 'A' && ch <= 'Z')
{
ch = (char)(ch + 13);
if(ch > 'Z')
{
ch = (char)(ch - 26);
}
}
System.out.print(ch);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment