Reputation:
#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:
Upvotes: 7
Views: 55526
Reputation: 609
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);
}
Upvotes: 2
Reputation: 1235
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);
}
Upvotes: 1
Reputation: 11
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);
}
Upvotes: 1
Reputation: 7598
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.
Upvotes: 3
Reputation: 101
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 !
Upvotes: 2
Reputation: 5490
(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])) {
.
Upvotes: 5
Reputation: 28865
Change
if ((cipher[i] += shift) >= 65 && (cipher[i] += shift) <= 90) ...
to
if ((cipher[i] + shift) >= 65 && (cipher[i] + shift) <= 90) ...
since +=
modifies cipher[i]
.
Upvotes: 9