2

You're getting a seg fault because the code tries to work with argv[1] before checking whether it exists. "Ready, Fire, Aim!" The very first thing that must be done is to check the value of argc. If it is NOT 2, the program should terminate immediately. Period. It shouldn't be built on an if/else structure and shouldn't encapsulate any other code. ...


2

isupper() does not work on strings, it only works on chars, that's why you get the seg fault. "I did not find anything that helped to iterate over the chars of p. Can you give me a hint on how to do this ? Meaning getting the chars of p, since isupper(p) does not work." Simply use a for loop that iterates over the whole string p (which you've already done ...


1

The problem lies here: int asciiValue = plaintext[i]+key%26; Simply put, it won't work at all. plaintext[i] contains an ASCII value for a letter. That's a number somewhere between 65 and 122 inclusive. Add a key to it and it's even larger. Now, what happens when you apply %26 to it? You ALWAYS get a number between 0 and 25 inclusive. In other words, you ...


1

You can't reprompt a user for command line arguments. You will have to exit the program after printing the usage.


1

Perhaps a review of the class material on command line arguments is in order? https://cs50.harvard.edu/x/2020/notes/2/#command-line-arguments


1

They are right, your program is only checking the first character of argv[1]. Try to think - your for loop checks the first character; if its not a digit, it throws an error and ends the program. Or if its a digit, it prompts for input. So basically you are stopping the program either way just after checking the first character! What you should do instead, ...


1

You have to submit from the directory where the files to submit are, like ~/workspace/pset2/caesar/ or wherever you put the caesar.c file. You can use cd command ("change directory") to make this your current directory, or open a new terminal via right-click on the directory in the tree view. The slug cs50/2018/x/caesar is not to be changed. Even though it ...


1

Your problem occurs here: for(int i = 0, n = strlen(plaintext); i <= n; i++) If we look at the first test case, plaintext is a, which has length of 1. Based on your for loop, you will allow keep incrementing i while it is less than or equal to 1. However, we have to recall how a string is stored in memory. In the case of a being stored as plaintext, we ...


1

There is a subtle but significant difference between what the spec requires and what your program produces. What the spec requires: Your program must output plaintext: (without a newline) and then prompt the user What your program produces: printf ("plain text:");


1

You have an extra (misplaced) {something} in some printf statement. What would cause every letter to print on a "new line"? Some unsolicited advice. Recall from the spec: So that we can automate some tests of your code, your program must behave per the below. Assumed that the underlined text is what some user has typed. username@ide50:~/...


1

You need to print a linefeed char '\n' after the chipertext. So it should be printf("%s\n", et); instead of printf("%s", et); If this solves your problem, please click the check mark to accept the answer.


1

cipher = (chr+key) % 26 So, if you enter, say d and a key of 5, then: cipher = ('d' + 5) % 26 cipher = (100 + 5) % 26 cipher = 1 1 is a non printable char. The formula for caesar given in the pset works on the assumption that your chars are in the range of 0 - 25 (not the ascii value). So you need to bring your 'd' down to that range (perhaps by ...


1

Ok, First have a look at the if condition that you are implementing for checking if the character is an alphabet. Let us assume the letter at a certain time that is being ciphered is 'M' and the key is let us say '5'. Now if you check it, the int conversion of 'M' as per ASCII is 77( see : www.asciitable.com ). Now take a look at what is happening inside ...


1

I'm not sure why you are getting those specific errors. Perhaps you are running an old version of your code? I have noticed recently that when a file in the IDE is saved, sometimes the file doesn't actually get updated, so the old code is recompiled. If you close the file and reopen it, you should see if it has the updated code. Reupdating and saving usually ...


Only top voted, non community-wiki answers of a minimum length are eligible