0

I tried submitting the solution for pset2 - caesar but I get a lot of errors back. Most of them are to do with wrong translation of the input text. Can anyone tell me where I'm going wrong ?

Thank you !

#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

int main (int argc, string argv[]){

//get key for the programme
int i;
for (i = 0; i < strlen(argv[1]); i++){
if ((argv[1][i] < '0')||(argv[1][i] > '9')){
    printf("Usage: ./caesar key\n");
    return 1;
}
} 

//turn key into int
string key = argv[1];
int k = atoi(key);


//get text
string plaintext = get_string("Please input your text: \n");

//use ci = (pi + k) % 26 to encipher the text

for (i = 0 ; i < strlen(plaintext) ; i++){
    if(isalpha(plaintext[i])){
        if (isupper(plaintext[i])){
        printf("ciphertext: %c", (((plaintext[i] + k) - 65) % 26) + 65);
    }

        if (islower(plaintext[i])){
        printf("ciphertext: %c", (((plaintext[i] + k) - 97) % 26) + 97);
    }
  } else {
      printf("ciphertext: %c", plaintext[i]);
  }

}

printf("\n");

}

1

I have to ask. Did you actually test your program or did you just run check50 or submit50? Or did you test with single letters and stop testing? Thorough testing of software is a critical skill.

If you test the program, the problem is obvious. Here's output from a test run:

~/go/test/pset2/ $ ./caesar 1
Please input your text: 
test
ciphertext: uciphertext: fciphertext: tciphertext: u

It's printing "ciphertext: " after every letter that's encoded when it should only be printed once at the beginning of the line.

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

1
  • Now that I think about it, I did not check the code after I added the ciphertext part and in my head it made sense. I tested it a lot before and it seemed good except it didn't print our ciphertext. Thank you for your help :)
    – Nomad
    Nov 12 '20 at 19:48

You must log in to answer this question.

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