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 › Pseudocode for unique key characters
- This topic is empty.
-
AuthorPosts
-
February 16, 2022 at 12:29 pm #221
Need to compare elements from argv[1] [0] to argv[1][26]//there are 26 alphabets (0 – 25) plus one for end of array.
Check if
1. check if argv[1][0] == argv[1][1]
2. If yes,
3. return 1,
4. else goto line 1
5. argv[1][0] == argv[1][1+1]
6. till argv[1][0] == argv[1][1+25]
7. // 26th alphabetic character argv[1][25] needs to be compared with argv[1][0]
8. Next, continue with argv[1][0+1] till argv[1][25] with line 1.
I believe, the above pseudocode will consider uppercase and lowercase characters different. That is okay for the time being though final project will need a different approach (uppercase plaintext will have ciphertext in upper).
Seeking guidance if the above is okay or needs correction.
Reply
Why not use the toupper() or tolower() function to get rid of case consideration. Otherwise the pseudocode seems fine. Ultimately leading to two loops.
Query
While the code could now spot duplicate keys, the next task is to ‘get rid of case consideration’.
Your advice to use either toupper() or tolower() means either all the keys to be converted to uppercase or lowercase?
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
for (int i = 0; i < 25; i++) { for (int j = i; j < 25; j++) { if(argv[1][j] == argv[1][j+1]) { printf("keys should be unique"); return 1; } } }
[/dm_code_snippet]
Reply
exactly
Query
Here is how I tried to convert all characters to uppercase:
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
for (int i = 0; i < 25; i++) { for (int j = i; j < 25; j++) { argv[1][j] = toupper(argv[1][j]); if(argv[1][j] == argv[1][j+1]) { printf("keys should be unique"); return 1; } } }
[/dm_code_snippet]
Query
With the above code, the program is compiling. I tried to check actual conversion to uppercase with printf:
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
for (int i = 0; i < 25; i++) { for (int j = i; j < 25; j++) { argv[1][j] = toupper(argv[1][j]); printf("%s",argv[1][j]); if(argv[1][j] == argv[1][j+1]) { printf("keys should be unique"); return 1; } } }
[/dm_code_snippet]
Getting format error.
Reply
printf(“%s”,argv[1][j]);
agrv[i]
is a string whileargv[i][j]
is a character. You should be using %c
Query
Though not part of the Substitution project, yet came across this scenario: The variable s of string type intended to be converted to uppercase.
string s = get_string(“plaintext: “);
s = toupper(s);
Is it not correct ?
Reply
again s is a string. toupper() works on chars
Reply
https://edstem.org/us/courses/176/discussion/1152834?comment=2624763[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.