4

Think about what this code is doing; int main(int argc, string argv[]) { int n = strlen(argv[1]); If there's no argument, then argv[1] doesn't exist. Trying to access it generates a seg fault. That's why it's necessary to check the value of argc before trying to use any argv element. If this answers your question, please click on the check mark to ...


2

The problem lies here: if (key_length != 26 || isalpha(argv[1])) The isalpha() function and all of it's cousin issomething() functions take a single char as input. If you try to give it a string (argv(1) is a string, not a char, even if it only has one char), it causes a seg fault. It's that simple. As for debug50, it is a wrapper around gdb. Think of it ...


2

The problem is this cipher key BBCCEFGHIJKLMNOPQRSTUVWXYZ should not perform the encoding, it should give an error. From the spec [emphasis added]: If the key is invalid (as by not containing 26 characters, containing any character that is not an alphabetic character, or not containing each letter exactly once), your program should print an error message ...


1

For the benefit of all, here's the problem with the atoi issue. Look at this line of code: atoi(rep[p]) += 1; The atoi function returns a value that must be stored somewhere. This line of code is trying to add 1 to... well... what? The integer value that would be returned by atoi is simply not being used. This is not the correct way to use the atoi ...


1

You're correct. The system is likely tripping up because you are providing an extra null character to the output. Try again without this line: printf("%c\n", plainArray[strlen(plainText)]); p.s. There is actually no need for the null character at all in this program. Null is used to tell the computer to stop parsing through a list of characters, but your ...


1

Is ouptut allocated correctly? It is declared here char output[strlen(i)]; as a char array, and used here printf ("ciphertext: %s\n", output); as a string. It is missing the "thing that makes a string a string", i.e. the null terminator '\0'. output 1) isn't allocated to include the null-terminator and 2) isn't null terminated.


1

The documentation for isalpha explains that the input should be an unsigned char. https://manual.cs50.io/3/isalpha By passing in a string instead of a single char, the underlying library code is misbehaving. Too bad it's not a compile error. Why debug50 behaves differently, there's probably different library code being executed. Many advanced things happen &...


1

encodedPhrase[i] = substitutionKey[j]; This code above is causing error because encodedPhrase[i] is trying to access memory space which is not allotted to it. Your code has even several logical errors such as: It cannot detect upper case letters in user input. It cannot detect for special characters or whitespace. You can use the below code for reference ...


1

Sometimes, an error or unpredictable condition can be masked or hidden by the debugger when you run the code in the debugger. This is the case here. I cannot tell you why it is failing, only that it does. When this happens, you need to start getting creative and try to more carefully follow what's happening to find the bug. The error here is very subtle. ...


1

The placement of return true and return false have to be reversed. check_char should be used before check_distinct_char in the main program. Change the check_char function like this: int count = 0; for (int i = 0, len = strlen(s); i < len ; i++){ if (isalpha(s[i])) { count++; } } if (count == 26){ return true; ...


1

I think that you should change the check_distinct_char function's return type to int. Then if (argc == 2 && check_distinct_char(argv[1]) == 0) If you don't want to do this the you can change return 1 to return FALSE and return 0 to return TRUE If this helps check the tickmark


1

For starters, you can't chain an entire series of comparisons together like that. It just doesn't work. C has no ability to stack all those comparisons together and figure out that you want to compare the first item to the 4th or last items. This calls for two nested for loops to walk through the list of items and to compare each item to all the other items ...


1

I don't think h = (g[k] % 90) + 65; is doing what you want it to do. If your cipher is ABCDEFG.... (just the actual alphabet), for the first time you go through that loop (i = 0, k = 0), h = (g[k] % 90) + 65; -> h = ('A' % 90) + 65; -> h = (65) + 65; -> 130. h += k; -> 130. Then, toupper(x[i]) == h will never be true, because 130 isn't the ASCII value ...


1

Put a nested loop to compare each letter to all the others. An example would be for (int i = 0; i < 26; i++) { for (int j = 0; j < 26; j++) { if(i == j) { } else { if(argv[1][i] == argv[1][j]) { } else { printf("Please enter a valid substitution key in the command-line. Your key must only contain 26 letters of the alphabet and they have to be unique....


1

simple. two nested for loops to compare each letter to every letter that follows.


1

Your code reads from the outside of cipher which contains unknown values because your cipher is not terminated by \0. Here is the link that you can debug memory errors or unexpected behavior in the future. The instruction for other C psets. I copy & pasted the error message here. Memory access error: reading from the outside of a memory space; ...


1

You've run into a common newbie problem. The issue lies in this code: if ( isalpha ( c ) != 'true' ) The issue is with the return value of isalpha and what you're comparing it to. Let's handle the latter first. Putting true in single quotes is a problem. I'm not even sure what that will evaluate to. IF you want to compare something to true or false, ...


1

I am not a Pro, too, but the first thing I see is the "isalpha(c) != "true" -> When I recall this pset correctly, isalpha() does not return "false" or "true" but 0 or 1. But if the statement would be wrong the compiler would complain. So it must be a logical issue. However, that would be my first approach to solve this. Currently it looks like (at least for ...


1

If you want to check whether all 26 alphabetical characters are present, you can check whether the characters are not repeated. It works the same way. This was my code. Take a look. Here I checked whether a character is repeated again. :) string repeated = key; //duplicating another equal string to check if same character repeats itself for (int ...


1

I'm going to suggest you an easier method. You can simply use the ctype.h library. The library provides you with a function called isalpha(). This function literally checks whether a string contains only alphabetical characters. And I do believe instead of s,l++ you better write them separately. Another thing that I think I found is you could edit your ...


1

You aren't handling the situation being tested: handles multiple duplicate characters in key You've tested whether all are alpha, and if the length is 26, but not if there are duplicate chars in the key. As such, check50 times out waiting for you to end the program with an error message.


1

Your get_string() has no arguments, while in the lectures we've been taught to always insert a prompt (in the form of a string) in between the parentheses. Try combining these two lines: printf("plaintext: "); char* plaintext = get_string(); into this one liner: char* plaintext = get_string("plaintext: ");


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