Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Caesar Shift Cipher: read in a STREAM of text, (not just a line, a stream of

ID: 3856096 • Letter: C

Question

C++ Caesar Shift Cipher: read in a STREAM of text, (not just a line, a stream of arbitrary length, ) a char at a time, and as you do so, shift each letter forward in the alphabet a certain number of places. The number of places is read from the command line in argc[1], and turned into an int with: int shift = atoi(argc[1]): Example: shift 13 Hello, Jack Urvyb, Wnpx READ THE COMMAND LINE FOR AN INTEGER IN ARGV [1] Run it through atoi() to convert the command line string to an integer: int shift = atoi(argy[1]): Here's the pseudocode: read in a character c if (c is a letter) { make c lowercase make c a number between 0 and 25 add the shift value to c mod c so it's between 0 and 25 again add a 'a' to c so it's a real ASCII letter again. } print c on the output

Explanation / Answer

#include<stdio.h>
int shift;

void encrypt(char *plain_text) {
   int i = 0;
   int value;
   for (; plain_text[i]!=''; i++)
       if ((plain_text[i] >= 'A' && plain_text[i] <= 'Z') || (plain_text[i] >= 'a' && plain_text[i] <= 'z')) {
           plain_text[i] = tolower(plain_text[i]);
           value = plain_text[i] - 'a';
           value = value + shift;
           if (value > 25)
               value = value - 26;
           else if (value < 0)
               value = value + 26;

           putc('a' + value, stdout);
       }
       else
           putc(plain_text[i], stdout);

}

int main(int argc, char* argv[]) {
   char data[100];
   shift = atoi(argv[1]);
   printf("Enter plain text: ");
   scanf("%[^ ]",data);
   printf("Cypher text: ");
   encrypt(data);
}

I have followed the algo provided by you. Since I followed the algo, it converts all characters to lower before procesing, so the output is always in lower case. If you want me to change the code to be case sensitive, let me know. I shall update it for you.