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

It's a subtle problem,so subtle that I wasn't able to find it. However, I believe that I understand where it lies. There's an old saying in programming, "KISS, Keep It Simple, Stupid!" I remind myself of this on just about every project I work on. It means that when the code gets too complex, its more likely to have bugs in it. I think that's what'...


2

It's failing because the code isn't handling invalid keys correctly. For the cases where the key contains a number or a duplicate letter, the program should terminate. Check50 is timing out because the code is waiting for user input that check50 isn't giving. The problem lies here: for (int i = 0; i < argc; i++) It looks like you don't have an ...


1

Have you actually tested the program with an invalid char, like a number, in the key? Try using the alphabet with a 2 instead of an a and see what it does. It should be incredibly obvious why the program "timed out" while check50 was waiting for it to end. The fix should be very obvious too. If not, leave a message. If this answers your question, ...


1

It's a common mistake. Look at this code (comments removed): int main(int argc, string argv[]) { string key = argv[1]; The first line in main is trying to read argv[1]. If no parameter is given, then argc = 1 and argv[1] doesn't exist. (FYI, argv[0] contains the program name as it was entered when the program was invoked.) Because argv[1] doesn't ...


1

When you do, string test = "random-text", it's pointing to chunk of read-only memory containing the string. You should instead store it on heap or stack frame to get both read/ write access. The reason this works string test = get_string() is because the input-string is stored on heap. NOTE: This is not the real implementation. It's a consice way ...


1

string s = "hello"; declares a string literal. By definition, it is immutable. If you try to change a char in that string, you will segfault. Have a look at my post about string literals for more info. Also, you'll learn more about this in Week 4, but your line string s_capital = s; does not make a copy of the string called s. It simply creates ...


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

The problem lies here: for(int i = 0; i < keyL - 1; i++) { for(int j = i; j < keyL; j++) { if(key[i] == key[j]) return 0; } } When the loop runs the first time, it will always fail because i == j at the start. It's comparing the first letter in the key to itself. It should start with j = i+1. There may be other ...


1

Think you just have to use 'make substitution'


1

I was able to fix my problem by appending an '\0' char at the end of my ciphertext. ciphertext[size] = '\0';


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

In effect it is not an ascii value for an alphabetical character, if, however, it is not what we are trying to do, but what we want is to substitute the m character for the corresponding one of the key. For example if the key is key: JTREKYAVOGDXPSNCUIZLFBMWHQ, and we want to encrypt the character m, we must use the character 12 in the key, that is, if we ...


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

To make a long story short (for me :-) ): extra characters in Vigenere pset2 2016 Instead you can type in "pset2 extra characters" as keywords for the search. I guess, it was the same problem. P.S.: If this helps you to solve "the problem", do not forget to check the thread as answered. Thx


1

Maybe this link is helpful: Do YOU know how to find a seg fault?? Advice to new programmers I've encountered that problem more than one time, too


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

I had the same problem (error for 2nd to sixth tests), added a null char ('\0') to my ciphertext array and everything went right.


1

Please do not use argv[1] before you checked argc == 2. That's the segmentation fault. I don't see you check for duplicate letters (consider different case). Two more failing test cases. Weirdest thing I see is string outro[1][strlen(plain)]; This is an array of strlen(plain) pointers (basically 8-byte variables holding a memory address). I have no idea ...


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