CS50 threads to aide as a supplementary resource › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2 › Keys should be unique: A project in itself!
- This topic is empty.
February 15, 2022 at 7:58 am #219adminKeymaster
For key, these are the criteria:
Only 26 characters
Those 26 characters should be alphabets
Each of the characters should be unique
I believe the toughest one is to ensure each of the characters should be unique. On doing Google Search, I see a lot of ways to make sure if a string has all unique characters.
Just to confirm, one of the above strategies need to be applied for the Substitution Project. If so, this will be a project in itself though should be a great learning experience.
Here is my rough pseudocode after reading replies:
Need to compare elements from argv to argv//there are 26 alphabets (0 – 25) plus one for end of array
argv == argv
else argv == argv + 1
till argv // 25th alphabetic character argv needs to be compared with 26th.
Next, continue with argv + 1 till argv with line 1.
et me simply put it like this. You have to compare each character of key with each character of key. Say your key is abcdd. How are you going to know if any of the character is repeated? You take first charater ‘a’ and compare it with each character of key except for the first. if a==key and so on. We would get to know a is not repeated. Now what about second characted b. we take b and compare it with other chars of key started from char next to b and onwards.
HINT: This “pseudocode” would hint you to use two loops.
For a moment forget about coding in C. If I gave you this string: “ABCAD” … how would you as the human check if there are any duplicates? Don’t cheat by simply looking at it and determine the 1st and 4th letter both are ‘A’ 🙂
How would you logically solve this?
Start with the first object argv.
The ASCII value of the first object is recorded – int argv.
The second object argv should not have ASCII value equal to one that is already recorded in argv.
The task then would be to compare ASCII of each entered character with all the earlier ones entered. If similar, then return 1. Else continue.
You don’t need to compare ASCII values, you can simply compare the characters 🙂
So in the example above you would take the first letter, ‘A’, and compare to each of the other letters.
When checking for ‘B’, you only need to focus on the other letters in the rest of the string, you already compared ‘B’ with the first letter.
Be aware that ‘A’ and ‘a’ in this case is considered the same … just as to add a little spice to the challenge 🙂
- You must be logged in to reply to this topic.