5 votes
Accepted

Segmentation fault in Substitution - Problem Set 2

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. ...
Cliff B's user avatar
  • 68.7k
3 votes

Segmentation error on pset2 substitution

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 (...
Cliff B's user avatar
  • 68.7k
3 votes
Accepted

Segmentation error on pset2 substitution

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 ...
juls's user avatar
  • 359
3 votes
Accepted

Pset 2 - Substitution - Error - handles multiple duplicate characters in key

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 ...
DinoCoderSaurus's user avatar
2 votes

How to reject repeated letters for Substitution in Problem Set 2

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]) ...
Vsjain's user avatar
  • 1,016
2 votes
Accepted

How to reject repeated letters for Substitution in Problem Set 2

simple. two nested for loops to compare each letter to every letter that follows.
Cliff B's user avatar
  • 68.7k
2 votes

pset2 substitution not working

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] % ...
Llenk's user avatar
  • 744
2 votes

During PSET 2: Substitution, I got an expected expression error for get_string. Please help

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("...
TLH's user avatar
  • 312
2 votes
Accepted

pset2 substitution checking if all the characters in the key are different

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 ...
Cliff B's user avatar
  • 68.7k
1 vote
Accepted

Check50 Substitution error

The answer is in plain sight in the check50 results. Simply put, every test that encodes the data has failed for the same reason. Don't even have to look at the code. This is a good lesson that a ...
Cliff B's user avatar
  • 68.7k
1 vote
Accepted

PSet2, Substitution, Unexpected Output - Nested FOR loops producing repeat iterations

It's a logic error - something is missing. ;-) The reason that it generates multiple prints for certain letters is hidden in the current logic. Think about what happens. The inner loop runs through ...
Cliff B's user avatar
  • 68.7k
1 vote

PSet2, Substitution, Unexpected Output - Nested FOR loops producing repeat iterations

One problem is: after a letter is substituted, and text[i] is changed, the j loop continues and might substitute again, depending on the key. After substitution program should break out of the j loop. ...
DinoCoderSaurus's user avatar
1 vote
Accepted

cs50 pset2 substitution

There appear to be several problems with the code. Check the website address generated by check50 for details. It fails to copy any non-alpha characters to the output array ct[]. Since those char ...
Cliff B's user avatar
  • 68.7k
1 vote
Accepted

pset2, problem SUBSTITUTION, Is there more than 1 way to check for repeated characters in the 26-character key?

Both methods described could work just fine. Another way is to check off a character from the alphabet as you loop over each character in the key. So if the first character in the key is 'B' for ...
Fuelled_By_Coffee's user avatar
1 vote
Accepted

Char repetition in key

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 ...
Cliff B's user avatar
  • 68.7k
1 vote
Accepted

cs50 pset2 substitution Segmentation error

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 ...
Crisp_Coder99's user avatar
1 vote
Accepted

Pset2 Substitution My code working when I use debugger and does not working thorough console or test. What am I doing wrong?

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 ...
Cliff B's user avatar
  • 68.7k
1 vote

Substitution Just can't handle invalid keys

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;...
Vsjain's user avatar
  • 1,016
1 vote

pset2 substitution unable to identify string which has character repetition

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 ...
Vsjain's user avatar
  • 1,016
1 vote
Accepted

PSET2 Substitution - Output seemingly correct, fails check50 (null character?)

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. ...
TLH's user avatar
  • 312
1 vote

Get help in Pset2 Substitution

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 ...
DinoCoderSaurus's user avatar
1 vote

Pset2 - Substitution - "Output not valid ASCII text" error despite correct cipher output

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 ...
stensal's user avatar
  • 450
1 vote
Accepted

pset2 substitution - isalpha doesn't seem to recognize letters

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 ...
Cliff B's user avatar
  • 68.7k
1 vote

pset2 substitution - isalpha doesn't seem to recognize letters

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 ...
ALL's user avatar
  • 78
1 vote

PS2 substitution

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 ...
Kavija Karunarathna's user avatar
1 vote

pset 2-substitution-error handles multiple duplicate characters in key timed out while waiting for program to exit

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 ...
curiouskiwi's user avatar
  • 18.7k

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