Caesar cipher in C. My plaintext won't shift and the cipher text outcome is identical to it [closed]

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
-4
down vote
favorite
I'm not sure what I'm doing wrong yet, but while I continue to figure it out, I'd love to get some input from others w/ experience. The goal is to cipher the plaintext using a key provided at the command line. I can't wrap my head around why my ciphertext comes out the same as my plaintext. I seem to have figured out case preserving and wrapping around the alphabet, but I'm stuck with this part. Included libraries: stdio.h, stdlib.h, string.h.
int main(int argc, string argv)
int e = 0;
string k;
string p;
int key;
if (argc == 2)
k = argv[1];
else
e++;
printf("Error: %in", e);
key = atoi(argv[1]);
p = get_string("plaintext: ");
for (int i = 0, n = strlen(p); i < n; i++)
if (p[n] >= 'a' && p[n] <= 'z')
p[n] = (p[i] + key - 'a') % 26 + 'a';
else if (p[n] >= 'A' && p[n] <= 'Z')
p[n] = (p[i] + key - 'A') % 26 + 'A';
printf("ciphertext: %sn", p);
return 0;
c caesar-cipher
closed as off-topic by vnp, Stephen Rauch, Deduplicator, 200_success, ÃÂìýÃÂñ ῥõῠJul 18 at 0:56
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â vnp, Stephen Rauch, 200_success, ÃÂìýÃÂñ á¿¥Ã栨Â
add a comment |Â
up vote
-4
down vote
favorite
I'm not sure what I'm doing wrong yet, but while I continue to figure it out, I'd love to get some input from others w/ experience. The goal is to cipher the plaintext using a key provided at the command line. I can't wrap my head around why my ciphertext comes out the same as my plaintext. I seem to have figured out case preserving and wrapping around the alphabet, but I'm stuck with this part. Included libraries: stdio.h, stdlib.h, string.h.
int main(int argc, string argv)
int e = 0;
string k;
string p;
int key;
if (argc == 2)
k = argv[1];
else
e++;
printf("Error: %in", e);
key = atoi(argv[1]);
p = get_string("plaintext: ");
for (int i = 0, n = strlen(p); i < n; i++)
if (p[n] >= 'a' && p[n] <= 'z')
p[n] = (p[i] + key - 'a') % 26 + 'a';
else if (p[n] >= 'A' && p[n] <= 'Z')
p[n] = (p[i] + key - 'A') % 26 + 'A';
printf("ciphertext: %sn", p);
return 0;
c caesar-cipher
closed as off-topic by vnp, Stephen Rauch, Deduplicator, 200_success, ÃÂìýÃÂñ ῥõῠJul 18 at 0:56
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â vnp, Stephen Rauch, 200_success, ÃÂìýÃÂñ á¿¥Ã栨Â
add a comment |Â
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
I'm not sure what I'm doing wrong yet, but while I continue to figure it out, I'd love to get some input from others w/ experience. The goal is to cipher the plaintext using a key provided at the command line. I can't wrap my head around why my ciphertext comes out the same as my plaintext. I seem to have figured out case preserving and wrapping around the alphabet, but I'm stuck with this part. Included libraries: stdio.h, stdlib.h, string.h.
int main(int argc, string argv)
int e = 0;
string k;
string p;
int key;
if (argc == 2)
k = argv[1];
else
e++;
printf("Error: %in", e);
key = atoi(argv[1]);
p = get_string("plaintext: ");
for (int i = 0, n = strlen(p); i < n; i++)
if (p[n] >= 'a' && p[n] <= 'z')
p[n] = (p[i] + key - 'a') % 26 + 'a';
else if (p[n] >= 'A' && p[n] <= 'Z')
p[n] = (p[i] + key - 'A') % 26 + 'A';
printf("ciphertext: %sn", p);
return 0;
c caesar-cipher
I'm not sure what I'm doing wrong yet, but while I continue to figure it out, I'd love to get some input from others w/ experience. The goal is to cipher the plaintext using a key provided at the command line. I can't wrap my head around why my ciphertext comes out the same as my plaintext. I seem to have figured out case preserving and wrapping around the alphabet, but I'm stuck with this part. Included libraries: stdio.h, stdlib.h, string.h.
int main(int argc, string argv)
int e = 0;
string k;
string p;
int key;
if (argc == 2)
k = argv[1];
else
e++;
printf("Error: %in", e);
key = atoi(argv[1]);
p = get_string("plaintext: ");
for (int i = 0, n = strlen(p); i < n; i++)
if (p[n] >= 'a' && p[n] <= 'z')
p[n] = (p[i] + key - 'a') % 26 + 'a';
else if (p[n] >= 'A' && p[n] <= 'Z')
p[n] = (p[i] + key - 'A') % 26 + 'A';
printf("ciphertext: %sn", p);
return 0;
c caesar-cipher
edited Jul 17 at 23:52
asked Jul 17 at 23:18
hkz
33
33
closed as off-topic by vnp, Stephen Rauch, Deduplicator, 200_success, ÃÂìýÃÂñ ῥõῠJul 18 at 0:56
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â vnp, Stephen Rauch, 200_success, ÃÂìýÃÂñ á¿¥Ã栨Â
closed as off-topic by vnp, Stephen Rauch, Deduplicator, 200_success, ÃÂìýÃÂñ ῥõῠJul 18 at 0:56
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â vnp, Stephen Rauch, 200_success, ÃÂìýÃÂñ á¿¥Ã栨Â
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
-4
down vote
accepted
Bugs:
1) You're using p[n] instead of p[i]
2) The top section with k and e serves no purpose.
Style:
1) Your variables need better (more descriptive) names.
Aside from the bug, the algorithm looks correct. As an improvement, having it take key / input file / output file as command line arguments would be more useful for most purposes. Alternately, having it take just the key as an argument, accepting input without a prompt (piped in) and outputting just the encrypted text (for piping out) would also work.
Thanks for pointing out the bugs and for providing the helpful suggestions. This was part of instructions for a course problem set, so I was following their requirements. The e variable was to return an error. I can see that I may be declaring it in the wrong order perhaps. Much appreciated!
â hkz
Jul 18 at 0:18
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
-4
down vote
accepted
Bugs:
1) You're using p[n] instead of p[i]
2) The top section with k and e serves no purpose.
Style:
1) Your variables need better (more descriptive) names.
Aside from the bug, the algorithm looks correct. As an improvement, having it take key / input file / output file as command line arguments would be more useful for most purposes. Alternately, having it take just the key as an argument, accepting input without a prompt (piped in) and outputting just the encrypted text (for piping out) would also work.
Thanks for pointing out the bugs and for providing the helpful suggestions. This was part of instructions for a course problem set, so I was following their requirements. The e variable was to return an error. I can see that I may be declaring it in the wrong order perhaps. Much appreciated!
â hkz
Jul 18 at 0:18
add a comment |Â
up vote
-4
down vote
accepted
Bugs:
1) You're using p[n] instead of p[i]
2) The top section with k and e serves no purpose.
Style:
1) Your variables need better (more descriptive) names.
Aside from the bug, the algorithm looks correct. As an improvement, having it take key / input file / output file as command line arguments would be more useful for most purposes. Alternately, having it take just the key as an argument, accepting input without a prompt (piped in) and outputting just the encrypted text (for piping out) would also work.
Thanks for pointing out the bugs and for providing the helpful suggestions. This was part of instructions for a course problem set, so I was following their requirements. The e variable was to return an error. I can see that I may be declaring it in the wrong order perhaps. Much appreciated!
â hkz
Jul 18 at 0:18
add a comment |Â
up vote
-4
down vote
accepted
up vote
-4
down vote
accepted
Bugs:
1) You're using p[n] instead of p[i]
2) The top section with k and e serves no purpose.
Style:
1) Your variables need better (more descriptive) names.
Aside from the bug, the algorithm looks correct. As an improvement, having it take key / input file / output file as command line arguments would be more useful for most purposes. Alternately, having it take just the key as an argument, accepting input without a prompt (piped in) and outputting just the encrypted text (for piping out) would also work.
Bugs:
1) You're using p[n] instead of p[i]
2) The top section with k and e serves no purpose.
Style:
1) Your variables need better (more descriptive) names.
Aside from the bug, the algorithm looks correct. As an improvement, having it take key / input file / output file as command line arguments would be more useful for most purposes. Alternately, having it take just the key as an argument, accepting input without a prompt (piped in) and outputting just the encrypted text (for piping out) would also work.
answered Jul 18 at 0:06
Errorsatz
3135
3135
Thanks for pointing out the bugs and for providing the helpful suggestions. This was part of instructions for a course problem set, so I was following their requirements. The e variable was to return an error. I can see that I may be declaring it in the wrong order perhaps. Much appreciated!
â hkz
Jul 18 at 0:18
add a comment |Â
Thanks for pointing out the bugs and for providing the helpful suggestions. This was part of instructions for a course problem set, so I was following their requirements. The e variable was to return an error. I can see that I may be declaring it in the wrong order perhaps. Much appreciated!
â hkz
Jul 18 at 0:18
Thanks for pointing out the bugs and for providing the helpful suggestions. This was part of instructions for a course problem set, so I was following their requirements. The e variable was to return an error. I can see that I may be declaring it in the wrong order perhaps. Much appreciated!
â hkz
Jul 18 at 0:18
Thanks for pointing out the bugs and for providing the helpful suggestions. This was part of instructions for a course problem set, so I was following their requirements. The e variable was to return an error. I can see that I may be declaring it in the wrong order perhaps. Much appreciated!
â hkz
Jul 18 at 0:18
add a comment |Â