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 ...


7

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


7

main in Python no longer has a special meaning, but is a regular function, its name only chosen to conform to conventions based in C. Try exit(1) instead of return 1. Relevant python docs: https://docs.python.org/3/library/sys.html#sys.exit


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; }


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

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 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

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


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

Modulo is working correctly, but you've overlooked something. The idea behind using modulo is this. When you represent letters as numbers between 0 and 25 inclusive, and add a number to shift the letter, it may go beyond 25. You can apply % 26 to wrap back to the 0 to 25 range. But it ONLY works if the letters are represented by numbers in that range. ...


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 ...


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 ...


4

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


4

@Cliff B's answer is almost correct except that it's not the semicolon that's breaking the syntax rule. it's the compound statement (the curly braces and the contents in between them). regardless of that being useless, it would be fine if you did if (condition); else { // do something } a statement that only contains a semicolon is referred to as a ...


4

Your formula is correct as well as the understanding of the formula, only that you are mixing two things in a wrong way, look carefully at the following expression: ((('s[i]' - 97) + n) % 26) + 97) What is s[i], and what is 's[i]'? I have to say that it is the first time that I encounter this situation, probably 's[i]' is interpreted by the compiler as a ...


4

The output does not match the spec. This plaintext : should be this plaintext:. The same with ciphertext:. There is no space between the word and the :. While this may seem "overly strict", it is good practice for when you write code for a boss or a customer and there is real money on the line.


4

It's a memory thing, as in you will get different results based on what is in memory.ctxt is declared as an array of chars. Here printf("ciphertext: %s\n", ctxt); is treating it like a string (viz the print format of %s). But what is the thing that makes a string a string? It is the terminating null byte. Don't forget the add one to ctxt before you try to ...


3

As others have said, you can reduce your loops by combining what they do into one loop. You don't need to store the user input, so you can modify it without worry. Other than that, I would just add that your condition checking could also be simplified. You do not have to check if a char is an alpha before you check if it is an upper case or lower case ...


3

You just need to add the return code "\n" at the end, the same way you added the rest of the characters.


3

Essentially, argv[] is an array of size argc that tells you the parameters your code was called with. For instance, look at the make function you've been using - it takes one parameter, the name of the file you want to compile. So inside make, it uses argv[1] to get the name of the file. Note that like the pset says, argv[0] is always the name of the ...


3

Nothing is misleading in the lectures, you interpreted them wrong. It should be noted that argv is itself not a string, it is collection of string type objects. Let me tell you the structure of parameter argv[](assuming that you wrote hello.c). argv[] = {"./hello", "This", "is", "CS50"} // this is correct and not argv[] = "./hello This is CS50" //this ...


3

In this problem set, you are not asked to get input from the user after the program starts but rather get it using command line arguments. command line arguments are basically just input that the user can provide before running the program by typing it after the program name (e.g. ./caesar 13). To allow your program to make use of such input, the "main" ...


3

Your program is not handling the case where the user does not provide a key. The array argv contains the command line arguments used to run the program. For example, the command "./caesar yxocll" will put put "./ceasar" into argv[0] and "yxocll" into argv[1]. A lack of argv[1] means that the program was run with the command "./caesar" as there is no second ...


3

The purpose of the formula is to calculate the new (cyphered) value for a given character on a string, considering a swift value. The formula has 4 components: ci = ciphered character pi = current position k = number of positions to be shifted 26 = number or alphabet characters There's is something important here. The formula assumes that the first ...


3

Simple fix. Look at the line: int letter = 'plaintext[i]' ; You are trying to put the integer (ASCII) value of a letter stored at plaintext[i] into the variable letter. The idea is right, but look at what you did. int letter = 'something' ; When you use the single quotes around something, it has to be a single character. If you had used double quotes, ...


3

One thing that sticks out about this is that you have special cases for spaces, commas, and exclamation marks. While I haven't tested your code directly, it looks like it wouldn't print characters like . or '. You really should just check if the character is alphabetical, and print every other character unaltered. When you create int c[i], you aren't just ...


3

if (argc != 2) { return 1; printf("try again\n"); } else {.... why else, it is an unnecessary statement, if (argc != 2), exit the program, but simply the next line of code is executed, the if-else statement is not appropriate in this case, addition it should be if (argc != 2) { printf("try again\n"); return 1; } second ...


3

int k = atoi(argv[1]); if (argc != 2) {........... You are assuming that argv [1] exists before checking that really exists, if we have more than two arguments your program will work well, but if you have only one argument are accessing a memory area to which you should not access and argv [1] "does not exist" hence your segmentation fault Si esto responda ...


3

You actually have two problems with seg faults. First, the one that you probably haven't seen yet. The code tries to assign argv[1] to key before verifying it exists on the next line. If you run the program without a key, it will seg fault because of this. Next is this: if(isalpha(text)) The isalpha() function takes a single character as input. The code ...


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