Hot answers tagged

20

Let's consider the encryption formula given in the problem set: cᵢ = (pᵢ + k) % 26 Here, as in the problem set,cᵢ represents the value of the encrypted or enciphered letter, pᵢ represents the value of the plain text letter, and k represents the value of the encryption key. The English alphabet has 26 letters. Let's represent those letters with the ...


14

Just imagine one day you get a "hashed password" that could be used to log into your girlfriend's email, what will you do? Is it possible to log into the email with that "hashed password"? Of course, NO, because the "hashed" (or "encrypted") password contains non-sense characters. Typing that encrypted string into the log-in window will lead to the error ...


8

The pset says: If your program is executed without any command-line arguments, with more than one command-line argument, or with one command-line argument that contains any non-alphabetical character, your program should complain and exit immediately, with main returning 1 (thereby signifying an error that our own tests can detect). [my emphasis] It's ...


8

and welcome to the SE community. As for your question, here goes the answer: This is really simple, to do so, use the modulus operator, i.e. the % operator. Te modulus operator returns the remainder when you divide one number by the another, for example, if you perform 200 % 82, it would return 36, in the same way, 100 % 100 would give 0 and so on. Now ...


8

the issue is that you started your code int main( int argc, string argv[]) { int k = atoi(argv[1]; . . . } /// the mistakes is in the above assignment, if the there is no argv[1] so the OS will report error and terminates the program so you have not to do k assignment to argv[1] unless you make sure there is argv[1] thanks Mohamed Abdeltawab


8

I really think that is because you didn't include the correct header file, toupper() is a function declared in ctype.h you need to include this header in the code. #include <ctype.h>


7

Oh, I almost forget. Those who are struggling with this particular set, I'd suggest the following: Once you can "print" one letter of the first name (e.g. using the [0] square brackets and the first char of the array) the "real" problem is to print the "ith" character AFTER the space bar given by the user, more clearly: The user types "John Doe" you wanna ...


6

The number of command-line arguments cannot be negative. In fact, it cannot even be < 1. See this answer for more details! If the number of command-line arguments is not 1 (i.e., argc != 2), your program should "print" an error message and "return" an error code of 1.


6

GAR, figured it out. I didn't take the details of "your program should yell at you" literally. This works now. if (argc != 2) { printf("YOU SCREWED UP!"); return 1; }


6

int c = strlen(argv); strlen(a_string) takes a string as an argument. You have used argv as an argument, an array of strings. If you wanted to get the length of a particular string in the array, for instance, the first parameter of the program invocation, you would use int c = strlen(argv[1]); Anticipating another issue, here's another example. If you ...


6

Your problem is that letters[i] is a char, but the crypt() function expects a char * (string) as its first argument. So what you really want to do is pass a string with a single character, not a single character. You need to pass this char array ['a', '\0'], not this char 'a'. A simple fix can be to declare a char array with a length of 2, with the second ...


6

crypt() is an encryption algorithm. It takes a plaintext string (a user password), and encrypts it, so that it can be stored in a file. char *crypt(const char *key, const char *salt) The key argument is the password to be encrypted, and salt is a two character string that 'tweaks' the algorithm's output. The output is a 13 character string. The first ...


6

This is integer division. When you divide two integers, the result is an integer again, truncating the result, ignoring its fractional part. You can turn it into floating point division by making at least one operand a floating point number, e.g. any of those: float L = (float)letters / words * 100; // using a typecast float L = 100f * letters / words; ...


5

Yeah it is somewhat of a joke, seeing that ROT26 basically replaces each letter with the 26th one away, resulting in the same letters! See: http://www.urbandictionary.com/define.php?term=ROT26 So in a sense it is correct to say In the real world, though, it’s probably best to use ROT26 because in the real world we don't normally encrypt our words when ...


5

Usually the remainder operator (i.e., %) is used for wrapping around something. The reason is simply because, mathematically, the remainder of dividing an integer a over an integer b is from 0 to b - 1. Meanwhile, if we have an array of size 5 (indexes: 0, 1, 2, 3, 4). Given x, an integer >= 0, we can securely get a valid index in our 5-element array in ...


5

By defaults, chars are signed values in the range -128 to 127. If, say, your plaintext[i] is 'x' (ie, 120) and you add a key of 10, you don't get 130. Instead, you get -126. Signed integers (chars) count like this, adding 1. 120 121 122 123 124 125 126 127 -128 -127 -126 One way around this is to cast your char to an unsigned char when you do your "if ...


5

You need to add your curly brackets (aka braces). Instead of if ( argc != 2 ) printf ("usage : ./caesar + key \n"); return 1; You should have: if ( argc != 2 ) { printf ("usage : ./caesar + key \n"); return 1; }


5

You should just run make caesar! :) Edit: Since you edited your question to a completely different one, a segmentation fault happens when you touch a memory location that you shouldn't touch. It's hard to tell what exactly causes this without seeing your code, but more likely is that you're passing a string instead of a char to a function that accepts a ...


5

You have popped with many questions at the same time. Lets start with the simplest one. What's the relation between return value of a function and command line arguments? You don't return 1 because you have 1 command line argument. The return value of any function(including main()) is almost independent of the arguments passed (unless you do something ...


5

The spec requires you to print a message to the user before returning 1. Looks like you aren't.


5

You must include "\n" at the end of the encrypted line. This goes for all the C psets.


5

if((i = 0) || isspace(s[i - 1])) Are you testing for i == 0 or reassigning a value of 0 to i, i=0 ? It's a common mistake. A single = is an assignment, while two == signs is a logical test of whether two things are the same. The result of the error is that every time you hit the if statement, you were resetting i to 0 and creating an infinite loop. You've ...


5

world, say hello! bazba zba zbazb ================= xoqmd, rby gflkp! You are doing this: world, say hello! bazbazbazbazbazb ================= xoqmd, szz gflkp! You are correctly skipping any non-alpha in the plaintext but you are still incrementing the key, because you have tied it to i which keeps incrementing even if the plaintext is non-alpha. ...


5

First of all you have to find the index in the alphabet of each letter in your plaintext. So if your plaintext is "abce" your indexes should be [0, 1, 2, 4] as A/a is 0 B/b is 1 ... Z/z is 25 You should use the ASCII table to find in what position of the table is each letter, and then subtract 'A' or 'a' to get the right index. Then to wrap around, you ...


5

The specifications say that you must print a prompt: Your program must output plaintext: (without a newline) and then prompt the user for a string of plaintext (using get_string). Your program must output ciphertext: (without a newline) followed by the plaintext’s corresponding ciphertext, with each alphabetical character in the plaintext "rotated"...


5

From the What's new? in CS50 2017 document: CS50 Library To be more consistent with stylistic conventions in C, we’ve renamed the functions in the CS50 Library as follows: GetChar is now get_char GetFloat is now get_float GetInt is now get_int GetLongLong is now get_long_long GetString is now get_string The old spellings still work (for now!) but best to ...


4

This is a very common error. You've forgotten to include a control character in your output, and it's not showing up in the message from check50 in Terminal because it's a non-printing character. It's easier to see the problem when you expand one of the failed check tasks in your sandbox link. For instance, the third check (first one to fail) has this line ...


4

In order to loop through the keyword, you need to use the modulo operator. For example, say your keyword is 5 characters long, while the text-to-be-enciphered is 10 characters long. At the beginning of your code you will have some for loop like this: for (int i = 0, n = strlen(plain), k = 0; i < n; i++, k++) Here, k is used to access consecutive ...


4

Doesn't isdigit only work on chars, rather than argv[1] which is a string?


4

You don't necessarily have to use isblank() here. Instead, there should be an "else" case that executes when the "ith" character of the plain text to-be-encrypted is neither an uppercase nor lowercase letter. It should look something like this: else { printf("%c", plain[i]); } ,assuming that plain is the name of the input string from the user that ...


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