enter image description here #include <stdio.h> #include <cs50.h> #include <math.h> #include <string.h> #include <ctype.h> #include <stdlib.h> //atoi ?

bool is_valid_key( string s);
// Validate the key as a string
// make sure each character is a digit
void encrypt(string plaintext, string chiphertext, int k);

int main (int argc, string argv[])
// Validate command line arguments
  if (argc != 2 || !is_valid_key(argv[1]))   // Valid key
      printf("Usage: ./caesar key");
      return 1;
  int k = atoi(argv[1]);   // #include <stdlib.h>....atoi
  string s =  get_string("plaintext: ");
  int n = strlen(s);
  char ciphertext[n + 1];
  encrypt(s, ciphertext, k );

  printf("ciphertext: %s/n", ciphertext);
    return 0;

      void encrypt(string plaintext, string ciphertext, int k)


      int i = 0;

      for (i = 0; i < strlen(plaintext);  i++ )

       char ch = plaintext[i];

       // ci = (pi + k) % 26
       //pi = current character
         char temp = tolower(ch);
         int pi = temp  - 'a';
         char ci = ((pi + k) % 26) + 'a';
         ciphertext[i] = islower(ch ? ci : toupper(ci));
           ciphertext[i] = ch;

        ciphertext[i] = '\0';

         bool is_valid_key( string s)

        //write a for loop function to validate string
        for (int i = 0;  i < strlen(s); i++)
            char ch = s[i];
            if (!isdigit(ch))
                return false;
        return true;

"This what I come up with...."

~/pset2/ceaser/ $ make ceaser clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow ceaser.c -lcrypt -lcs50 -lm -o ceaser ~/pset2/ceaser/ $ ./ceaser 1 plaintext: a ciphertext: /n~/pset2/ceaser/ $

  • It compiles fine. (Of course, that doesn't say anything about whether it works.) What are the problems that you are having? Please edit the question and show exactly what errors you are getting.
    – Cliff B
    Commented Oct 22, 2020 at 19:36
  • ~/pset2/ceaser/ $ make ceaser clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow ceaser.c -lcrypt -lcs50 -lm -o ceaser ~/pset2/ceaser/ $ ./ceaser 1 plaintext: a ciphertext: /n~/pset2/ceaser/ $ Commented Oct 22, 2020 at 20:30
  • Compiled fine. Execution, not so much. Please edit your question (not add a comment) and add more details about what it is doing wrong, what you think is wrong, exampled of output, etc. Please don't add as comments. In comments, they're difficult to read or understand exactly what's happening because of the forum's formatting.
    – Cliff B
    Commented Oct 22, 2020 at 20:34
  • As an unrelated side note you've spelled Caesar wrong which is bound to cause errors with cs50 commands like style50 or check50 Commented Oct 24, 2020 at 8:18

2 Answers 2


So, it's clear that the program compiles without errors (that big message is just an echo of the specific compiler commands and all the flags that are set.) It's also clear that it executes and completes execution.

The problem is that it is not encoding the plain text.

Your code has two significantly serious problems. The first is that the code calls a function to actually encode the plain text. But it has a return type of void. The encoded text is never returned to the main program to be processed or printed. Note also that variables are passed by copy, not by reference. That means that copies of all the parameters are created and passed into the function. The function does NOT change any of those vars in the calling program. It only changes the copies in the function itself. Those local vars in the function are destroyed when execution returns to the calling code. In essence, the plaintext is encoded and then discarded.

The second problem is that the code inside the encrypt() function simply isn't encoding the letters correctly. I recommend reviewing the video short on how to encode the letters.

BTW, you still need to learn the difference between a forward slash, "/" and a backslash, "\". The backslash is used in combination with the letter n, "\n" to create a line feed. This,"/n" will print exactly that, a forward slash and the letter n, which is exactly what you are getting in your output.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

  • "\" was definitely one of the problem the other one was I didn't have a parentheses ().. ciphertext[i] = islower(ch ? ci : toupper(ci)); ...it should be (ch) THANK Cliff B Commented Oct 23, 2020 at 17:30

back slash forward slash \ / was definitely one of the problem the other one was I didn't have a parentheses ().. ciphertext[i] = islower(ch ? ci : toupper(ci)); ...it should be (ch)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .