IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2: [Arrays] – Functions, Variable and Scope, Debugging, Arrays, and Command Line Arguments › Keys should be unique: A project in itself!
Tagged: strings, substitution project, unique string
- This topic is empty.
-
AuthorPosts
-
February 15, 2022 at 7:58 am #219
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.
https://www.quora.com/How-do-I-determine-if-a-string-has-all-unique-characters
https://www.geeksforgeeks.org/determine-string-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.
Update:
Here is my rough pseudocode after reading replies:
Need to compare elements from argv[0] to argv[0][26]//there are 26 alphabets (0 – 25) plus one for end of array
Check if
-
argv[0][0] == argv[0][1]
-
If yes,
-
return 1,
-
else argv[0][0] == argv[0][0] + 1
-
till argv[0][24] // 25th alphabetic character argv[0][24] needs to be compared with 26th.
-
Next, continue with argv[0][0] + 1 till argv[0][24] with line 1.
Reply
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[1] 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?
Query
Start with the first object argv[1][0].
The ASCII value of the first object is recorded – int argv[1][0].
The second object argv[1][1] should not have ASCII value equal to one that is already recorded in argv[1][0].
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.
Reply
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 🙂
[learn_press_profile]
-
-
AuthorPosts
- You must be logged in to reply to this topic.