7
#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 there's something wrong with my statements.

For Example:

  • Input: ABCD, 1 shift
  • Output: DEFG <= which is actually 3 shifts.
asked Dec 11, 2011 at 10:34
0

7 Answers 7

9

Change

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

to

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

since += modifies cipher[i].

answered Dec 11, 2011 at 10:47
Sign up to request clarification or add additional context in comments.

Comments

5

(I got here via http://codereview.stackexchange.com , so I'm still wearing a code review hat).

With code that manipulates letters, I find it easier to understand if it uses actual letters in the source, rather than numeric codes. So I recommend changing

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

to something like

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

Most people doing Caesar ciphers convert only the letters, and pass through punctuation, numbers, spaces, etc. unchanged. You might consider including the 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])) {

.

answered Jan 13, 2012 at 20:15

Comments

3
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 will ignore anything that is not an uppercase letter.

If the letters had codes 0-25, it would be very easy to do a shift because we could save a condition using just a remainder of 26. Which is what i did, i subtract 'A', so that the A letter will be 0, then i add the shift and calculate the remainder, so that if you add 1 to a 'Z', you will get an 'A' again and so on. And in the end i had to add 'A' again, because in ASCII, the 'A' is not really 0.

answered May 4, 2013 at 13:18

Comments

3

Small working version, no shift range except 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);
}
answered Oct 16, 2017 at 8:48

Comments

2

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 modify value whenever its evaluated. this was evaluated 3 times in your code, that's why it gives 3 shifts !

answered May 4, 2013 at 12:48

Comments

1

I modified LtWorf to match 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);
}
answered Feb 10, 2017 at 6:30

Comments

1

Caesar 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);
}
answered Aug 4, 2017 at 17:51

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.