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. ...
- 66.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 (...
- 66.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 ...
- 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 ...
- 27.5k
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])
...
- 1,002
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.
- 66.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] % ...
- 734
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("...
- 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 ...
- 66.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 ...
- 66.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 ...
- 66.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. ...
- 27.5k
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 ...
- 66.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 ...
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 ...
- 66.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 ...
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 ...
- 66.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;...
- 1,002
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 ...
- 1,002
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. ...
- 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 ...
- 27.5k
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 ...
- 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 ...
- 66.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 ...
- 88
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 ...
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 ...
- 18.5k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
substitution × 30pset2 × 25
pset2-substitution × 10
cs50x × 4
c × 3
check50 × 2
problemset2 × 2
ascii × 2
segmentation-fault × 1
error × 1
string × 1
fails-check50-but-works × 1
help × 1
week2 × 1
cs50x2020 × 1
debug50 × 1
output × 1
segmentation × 1
isalpha × 1
getstring × 1
cs50-pset2 × 1
fault × 1
expected-expression × 1
toupper × 1
tolower × 1