Amit Singh
Amit Singh

Reputation: 1800

Ceaser's Cipher

Here's a Java Code for Ceaser's cipher

import java.util.*;

public class Main {

public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int length = stdin.nextInt();
    String text = stdin.next();
    int shift = stdin.nextInt();

    for(int i = 0; i < length; i++) {
        char c = text.charAt(i);
        if(c >= 'a' && c <= 'z') {
            System.out.print((char)(((int)c - (int)'a' + shift) % 26 + (int)'a'));
        } else if(c >= 'A' && c <= 'Z') {
            System.out.print((char)(((int)c - (int)'A' + shift) % 26 + (int)'A'));
        } else {
            System.out.print(c);
        }
    }
    stdin.close();
}
}

and i cannot understand what is happening at this line of code

System.out.print((char)(((int)c - (int)'a' + shift) % 26 + (int)'a'));

why do -( int ) ' a '

Upvotes: 1

Views: 254

Answers (4)

Moiz Ahmed
Moiz Ahmed

Reputation: 11

#include<iostream>
#include<string>
using namespace std;
int Ceasers_Cipher()
{
    int y;
    string x;
    int r;
    cout << "enter any sentence" << endl;
    getline(cin, x);
    y = x.size();
    cout << "enter rotation" << endl;
    cin >> r;
    for (int i = 0; i < y; i++)
    {
        if (x[i] >= 'a' && x[i] <= 'a'+(26-r))
        {
            x[i] += r;
        }
        else if (x[i] >= 'a' + (26 - r)+1 && x[i] <= 'z')
        {
            x[i] -= 26-r;
        }
        else if (x[i] >= 'A' && x[i] <= 'A' + (26 - r))
        {
            x[i] +=r;
        }
        else if (x[i]>='A' + (26 - r) + 1 && x[i] <= 'Z')
        {
            x[i] -= 26-r;
        }
    }
    cout << x << endl;
    return 0;
}
int main()
{
    Ceasers_Cipher();
}

Upvotes: 0

Max Hampton
Max Hampton

Reputation: 1304

In order for the % 26 to correctly rotate the encoded character when the shift would push it beyond 'z', you need to be dealing with values 0-25. The ASCII values of 'a' - 'z' are 97-122, making the rotation difficult. By subtracting 'a' from the character being shifted, you are mapping the character to a value from 0-25, making the rotation possible using % 26.

Upvotes: 3

yd1
yd1

Reputation: 277

as "Max Hampton" said, the values of the letters in the ASCII table starts from 97 (in the small letters case).

so therefore if for example you have the letter c='p' then c = 112. also 'a'=97, so 'p'-'a' = 112-97 = 15 (note: p is place 16 in the alphabet).

now we add the shift (although p is now moved 1 step back, be we will fix it in a moment). lets the the shift is 3 (we want p->s)

now we got 15+3 = 18. 18%26 = 18.

now for the fix: 18 + 'a' = 18+97 = 115 = 's' (the 1 is back here)

and done :)

Upvotes: 1

Shreyas Sarvothama
Shreyas Sarvothama

Reputation: 485

Its the ASCII values.. letter a has ascii value of 97, A has ascii value of 65.

I hope you understand how ceaser cipher works.

if you have ABCD as your original text and you want to do a shift of 1 to apply ceaser cipher, it means A will be B, B will be C, C will be D and D will be E.

length is your string length, text is your original text, shift is by how many shifts in alphabets you want to apply ceaser cipher.

Lets take a sample text: abcd

with shift 1

for now lets assume c value is 'a'

so this statement is (int)c - (int)'a' + shift) % 26 + (int)'a')

will typically do (97-97+1)%26+97

(1%26)+97
1+97 
98

which is ascii equivalent of b. that is why in your code the entire operation is converted to char at the end:

**(char)**(((int)c - (int)'a' + shift) % 26 + (int)'a')

Hope it makes sense

Upvotes: 1

Related Questions