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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
-4
down vote

favorite
1












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;







share|improve this question













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, πάντα ῥεῖ
If this question can be reworded to fit the rules in the help center, please edit the question.


















    up vote
    -4
    down vote

    favorite
    1












    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;







    share|improve this question













    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, πάντα ῥεῖ
    If this question can be reworded to fit the rules in the help center, please edit the question.














      up vote
      -4
      down vote

      favorite
      1









      up vote
      -4
      down vote

      favorite
      1






      1





      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;







      share|improve this question













      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;









      share|improve this question












      share|improve this question




      share|improve this question








      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, πάντα ῥεῖ
      If this question can be reworded to fit the rules in the help center, please edit the question.




      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, πάντα ῥεῖ
      If this question can be reworded to fit the rules in the help center, please edit the question.




















          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.






          share|improve this answer





















          • 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

















          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.






          share|improve this answer





















          • 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














          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.






          share|improve this answer





















          • 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












          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.






          share|improve this answer













          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.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          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
















          • 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


          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods