0
package casesar02;

import java.util.Scanner;

public class casesar02

{


public static void main(String[] args)

{

          Scanner scan = new Scanner(System.in);
          int key = 4;
         // scan.nextLine();
         String str = scan.nextLine();


         String encrypted = encrypt(str, key);
         System.out.println(encrypted);
        }

    public static String encrypt(String str, int key) {

        String encrypted = "";

        for (int i = 0; i < str.length(); i++) {

            int c = str.charAt(i);

            if (Character.isUpperCase(c)) {

                if(c < 'C')
                {

                    c = c + 26;

                }

                c = c - (key % 26);

            } else if (Character.isLowerCase(c)) {

                if(c < 'c')
                {

                    c = c + 26;

                }

                c = c - (key % 26);

            }

            encrypted += (char) c;

        }

        return encrypted;
    }
}

Input
XLMW MW MRHCFSC. M EQ LIVIFC SVHIVMRK XLEX EPP QC QIR QYWX IEX TITTIVSRM TMDDEW IZIVCHEC. XLMW SVHIV AMPP FI VITIEPIH SRPC YTSR QC VIXMVIQIRX. PSRK PMZI XLI GLMTQYROW!
Output
THIS IS IND?BO?. I AM HEREB? ORDERING THAT ALL M? MEN MUST EAT PEPPERONI PI@@AS EVER?DA?. THIS ORDER WILL BE REPEALED ONL? UPON M? RETIREMENT. LONG LIVE THE CHIPMUNKS!

Why did not output
THIS IS INDYBOY. I AM HEREBY ORDERING THAT ALL MY MEN MUST EAT PEPPERONI PIZZAS EVERYDAY. THIS ORDER WILL BE REPEALED ONLY UPON MY RETIREMENT. LONG LIVE THE CHIPMUNKS!
I miss What??
Please Tell me

Edited by Reverend Jim: Fixed formatting

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by VernonDozier
0

This is not the same code you had at your previous thread. Why did you changed it?

Also why are you checking this:
if(c < 'C')
BEFORE you call this:
c = c - (key % 26)

Also why are you using this character: 'C'. I already explained what you should put there

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.