Tag Info

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. ...
• 69.2k
Accepted

Pset 2 Substitution Segmentation Fault

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, ...
• 69.2k
Accepted

CS50 pset2 substitution - timed out while waiting for program to exit

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 ...
• 69.2k

Why do i get these errors? i am new to programming so please be easy on me. The errors are in the body

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

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,016
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.
• 69.2k
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 ...
• 69.2k
Accepted

Different results with check50

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 ...
• 69.2k
Accepted

Why am I getting a segmentation fault (pset2 substitution)?

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 ...
• 18.7k
Accepted

Can't figure out why check50 sad faced these two

Sorry, but the output is not the same. Here's what happens when I ran the first failed test case: ~/go/test/substitution/ \$ ./substitution yukfrnlbavmwzteogxhcipjsqd plaintext: This is CS50 ...
• 69.2k
Accepted

PSET2 Substitution

Did you remember to add one extra char to the length of ciphertext and to insert the end of string marker in that last space? Also, shouldn't the length of ciphertext be based on the length of string ...
• 69.2k
1 vote
Accepted

CS50 Pset2 Substitution "expected prompt for input, found none"

Be very careful here if (argv[k] == argv[r]) argv is the array of command line arguments. As such, argv[0] is ./substitution, argv[1] is the key, and argv[2] hits memory that doesn't belong to your ...
• 18.7k
1 vote
Accepted

Segmentation fault (core dumped) after main returns 0

It appears to be seg faulting during program exit and memory cleanup/trash collection. I can't explain why. Perhaps the error below is pointing at an invalid or out of bounds memory address, but the ...
• 69.2k
1 vote

What are these weird symbols and why am i getting them? (Wk 2 Substitution)

It's a common newbie error. The problem lies here: //Length of plaintext int n = strlen(plaintext); //Declaring ciphertext variable char ciphertext[n]; This doesn't allow room for the end of string ...
• 69.2k
1 vote
Accepted

CS50 pset2 substitution: Segmentation fault in some of "This is CS50" tests

I've left the bad code. If you notice, you only fail check50 when you have a some/all lowercase chars in the key. What might happen in your code when, say, the key char is 'z'? 'z' - 65 is 57. ...
• 18.7k
1 vote
Accepted

pset2: Check50 on Substitution problem fails when output is the exact same

Non-printing characters. If you want to "see" what check50 "sees" try running the first example this way: ./substitution ZYXWVUTSRQPONMLKJIHGFEDCBA | cat -v This pipes (|) the ...
• 28.6k
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

Pset 2 (Substitution), time out (handling of incorrect chars)

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 ...
• 69.2k
1 vote

PSET2 Substitution Segmentation Fault. Works fine with get_string

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. ...
• 306
1 vote
Accepted

CS50 PSET3 Substitution replacing some letters incorrectly

The problem here arises from a nuance of memory management: string ciph = plain; When you declare this variable, you are not creating a copy of plain; rather, you create a second pointer pointing to ...
• 950
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,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 ...
• 1,016
1 vote
Accepted

Trying to do pset 2, but it doesn't even accept my input. What am I doing wrong?

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, ...
• 69.2k
1 vote
Accepted

Clang can't find -lcs50.h

Think you just have to use 'make substitution'
• 90
1 vote
Accepted

pset 2 - Substitution - "output not valid ASCII text" error

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

pset2 substitution

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 ...
• 5,179
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 ...
• 69.2k
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 ...
• 78
1 vote
Accepted

Pset2 - Substitution: Output seemed correct, failed on some check50

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

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