Caesar Code Cipher

#include <stdio.h> void caesar (char cipher[], int shift); int main () { char cipher[50]; int shift; printf("Enter text to be encrypted IN CAPITAL LETTERS ONLY: "); scanf("%s", cipher); printf("How many shifts do you prefer? 1-10 only: "); scanf("%d", &shift); caesar (cipher, shift); return 0; } void caesar (char cipher[], int shift) { int i = 0; while (cipher[i] != '\0') { if ((cipher[i] += shift) >= 65 && (cipher[i] += shift) <= 90) { cipher[i] += (shift); } else { cipher[i] += (shift - 25); } i++; } printf("%s", cipher); } 

I'm starting to get encrypted outputs, but I'm afraid something is wrong with my statements.

Example:

  • Input: ABCD, 1 shift
  • Output: DEFG <=, which is actually 3 shifts.
+4
source share
7 answers

Edit

 if ((cipher[i] += shift) >= 65 && (cipher[i] += shift) <= 90) ... 

to

 if ((cipher[i] + shift) >= 65 && (cipher[i] + shift) <= 90) ... 

since += modifies cipher[i] .

+7
source

(I got here through http://codereview.stackexchange.com , so I still wear a code review hat).

With code that manipulates letters, it’s easier for me to understand if it uses the actual letters in the source, rather than numeric codes. Therefore, I recommend changing

  cipher[i] += (shift - 25); 

to something like

  cipher[i] += (shift - ('Z' - 'A')); 

Most people making caesar ciphers only convert letters and omit punctuation, numbers, spaces, etc. without changes. You might consider including a standard character library.

 #include <ctype.h> 

and using the functions isalpha (), islower (), isupper () - in particular, changing

 if ((cipher[i]) >= 'A' && (cipher[i]) <= 'Z') { 

to something like

 if (isupper(cipher[i])) { 

.

+4
source
 void caesar (char cipher[], int shift) { int i = 0; while (cipher[i] != '\0') { if (cipher[i] >= 'A' && cipher[i]<='Z') { char newletter = cipher[i] - 'A'; newletter += shift; newletter = newletter % 26; cipher[i] = newletter + 'A'; } i++; } printf("%s", cipher); } 

This ignores anything that is not a capital letter.

If the letters had codes 0-25, it would be very easy to make a shift, because we could save the condition using only the remainder of 26. This is what I did, I subtract "A" so that the letter A is 0, then I add a shift and calculate the remainder, so if you add 1 to "Z", you get "A", again and so on. And in the end, I had to add β€œA” again, because in ASCII, β€œA” is actually not 0.

+2
source

try replacing

 if ((cipher[i] += shift) >= 65 && (cipher[i] += shift) <= 90) { cipher[i] += (shift); } else { cipher[i] += (shift - 25); } 

with

 if ((cipher[i] += shift) >= 65 && (cipher[i] = shift) <= 90) { // do nothing } else { cipher[i] = 'A' + ('Z' - cipher[i]) -1; } 

using "+ =" will change the value whenever it is evaluated. it has been rated 3 times in your code, so it gives 3 shifts!

+1
source

I changed LtWorf according to my current project.

shift range: -9 ~ +9

 void caesar(char cipher[], int shift) { int i = 0; while (cipher[i] != '\0') { if (cipher[i] >= 'A' && cipher[i]<='Z') { char newletter = cipher[i] - 'A' + 26; newletter += shift; newletter = newletter % 26; cipher[i] = newletter + 'A'; } else if (cipher[i] >= '0' && cipher[i]<='9') { char newletter = cipher[i] - '0' + 10; newletter += shift; newletter = newletter % 10; cipher[i] = newletter + '0'; } i++; } printf("%s\n", cipher); } 
0
source

Cesar Cipher Code for C ++

 #include<iostream> #include<cctype> using namespace std; char cipher(char c, int k) { if(isupper(c)) return char( 'A' + (c - 'A' + k ) % 26); else if(islower(c)) return char( 'a' + (c - 'a' + k ) % 26); else return c; } int main() { int k; string str; cin>>str>>k; int l = str.size(); for(int i=0; i<l; i++) cout<<cipher(str[i], k); } 
0
source

Small working version, without shift range, except for negative values:

 #include <stdio.h> void caesar (char cipher[], int shift); int main () { char cipher[50]; int shift; printf("Plaintext (Caps only): "); scanf("%s", cipher); printf("Shift: "); scanf("%d", &shift); caesar (cipher, shift); return 0; } void caesar (char cipher[], int shift) { for(int i=0; cipher[i] != '\0'; i++) cipher[i] = 65 + (cipher[i]-65+shift)%26; printf("Cipher text: %s\n", cipher); } 
0
source

Source: https://habr.com/ru/post/1385729/


All Articles