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 ...
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 ...
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]
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 ...
the issue is that you started your code
int main( int argc, string argv)
int k = atoi(argv;
/// the mistakes is in the above assignment, if the there is no argv so the OS will report error and terminates the program
so you have not to do k assignment to argv unless you make sure there is
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  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 ...
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.
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);
Anticipating another issue, here's another example. If you ...
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 ...
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 ...
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; ...
Yeah it is somewhat of a joke, seeing that ROT26 basically replaces each letter with the 26th one away, resulting in the same letters!
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 ...
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 ...
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.
One way around this is to cast your char to an unsigned char when you do your "if ...
You should just run make caesar! :)
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 ...
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 ...
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 ...
world, say hello!
bazba zba zbazb
xoqmd, rby gflkp!
You are doing this:
world, say hello!
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.
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 ...
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"...
From the What's new? in CS50 2017 document:
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 ...
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 ...
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 ...
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:
,assuming that plain is the name of the input string from the user that ...