0

I've figured out how the caesar cipher should work in theory, i.e take away the ascii value of 'A', then add they key to the current number under consideration, mod it by 26 then re- add the value of 'A' in order to encipher > in the case of an uppercase letter and have most of the code, however, I think I may be missing something that is causing the output to contain strange characters such as '/'

Here is my code:

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

int main(int argc, string argv[])

//ci = (pi + k) mod 26 ** pi is plaintext, the ith character in p, and k is key

{
    //get the key from the user


    if  (argc != 2)
        {
            printf("Error, invalid input\n");
        return 1;
        }

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


    if (k < 0)
        {
            printf("Error, invalid input\n");
            return 1;
        }



    else
        {
            //prompt user for the string to encode
            //printf("\n");
            string s = get_string();
            //printf("%s",s);

            //loops through length of string s
            for (int i = 0,  length = strlen(s); i  <  length;  i++)
            {

                if (isalpha(s[i]))  //checks if in alphabet

                    {

                    //printf("%c", s[i]);



                    if (isupper(s[i]))  //returns true if upper case
                            {

                                printf("%c", (( s[i] - 'A' + k) % 26) + 'A');

                            }   

                    if (islower(s[i])) //returns true if lower case

                            {

                                printf("%c", (( s[i] - 'a' + k) % 26) + 'a');

                            }







                    }




                    else


                                printf("%c", s[i]);



            }



        }






    printf("\n");
    return 0;
} 

1 Answer 1

1

The dreaded semi-colon!

Look at this line: if (islower(s[i]));

Remove that semi-colon, and you will see happy green smiley faces when you run check50.

On another note, you don't need to nest your algorithm within an if-else condition. Since your if condition returns 1 in the event that it is called, your program will automatically end at that point. The if condition by itself suffices without the else.

Also, it might be beneficial to learn the use of style50 along with check50. It's great for code readability and adherence to the cs50 style guide.

Update: The version of check50 for Fall 2016's iteration of CS50 requires a slight change in caesar. Your program's output must look like the following:

$ ./caesar 13
plaintext:  hello, world
ciphertext: uryyb, jbeyq

In past years, a correct program would only need the following output:

$ ./caesar 13
hello, world
uryyb, jbeyq
9
  • The error I'm having now (It's not a new error, one I've seen before) is "expected output, not a prompt for input" - I know this is something to do with the get_string function but right now I'm a bit unclear as to where to reposition it. Jan 23, 2017 at 17:31
  • Can you edit your code above to reflect your current version?
    – Peter
    Jan 23, 2017 at 17:36
  • 1
    I ran update50, and still no change. That is strange. Any idea what could be causing my IDE to return such a response? I think for now I will consider this problem 'passed' and move onto the next. Jan 23, 2017 at 17:58
  • 1
    Make sure your program conforms to the current spec. Specifically 1) " Your program must output plaintext: (without a newline) and then prompt the user for a string of plaintext (using get_string)." and 2) " Your program must output ciphertext: (without a newline) followed by the plaintext’s corresponding ciphertext....." Jan 23, 2017 at 18:37
  • 1
    @Peter maybe add info to that effect to the answer so Andrew can accept it. Jan 23, 2017 at 18:59

You must log in to answer this question.

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