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 › Rotating alphabetical characters by 26
Tagged: Caesar project
- This topic is empty.
-
AuthorPosts
-
January 19, 2022 at 4:49 am #153
[dm_code_snippet background=”no” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include<stdio.h> #include<cs50.h> #include<string.h> #include<ctype.h> int main(void) { string name = get_string("Enter: "); printf("Entered text by user: %s\n", name); int n = strlen(name); printf("lenght of entered text: %i\n", n); int key = get_int("enter key: "); for (int i = 0; i < n; i++) { if (isalpha(name[i])) { if (name[i] + key >= 91) { name[i] = (name[i] + key - 26); } else { name[i] = (name[i] + key); } } else { name[i] = name[i]; } } printf("cipher text: %s\n", name); }
[/dm_code_snippet]
Above is my code for uppercase characters (I am assuming for the time being the user enters only uppercase characters and not lowercase) only which is still faulty (https://www.canva.com/design/DAE1xXhyoXk/ba5X0Pf7gSAkmh9LdkbuOQ/view?utm_content=DAE1xXhyoXk&utm_campaign=designshare&utm_medium=link&utm_source=sharebutton).Up to key 44 or {72 which is ASCII of H + 44) = 116, the code working. So now perhaps need to introduce % operator correctly for rotation between 65 to 90.
Reply
1 % 26 = 1 , essentially if k is < n:
k % n = k
But
How about n % n = ?
Or: n + 1 % n = ? n + 2 % n = ? n + 3 % n = ? …
You should even try out: 2n % n 2n + 1 % n …
Try it out! See the pattern? Good luck!
Thanks.
Though still far from coming out with the pattern, here is my intermittent code:
[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”]
#include<stdio.h> #include<cs50.h> #include<string.h> #include<ctype.h> int main(void) { string name = get_string("Enter: "); printf("Entered text by user: %s\n", name); int n = strlen(name); printf("lenght of entered text: %i\n", n); int key = get_int("enter key: "); for (int i = 0; i < n; i++) { if (isalpha(name[i])) { if (name[i] + key > 90 && name[i] + key < 143) { name[i] = (name[i] + key - 26); } if (name[i] + key > 142 && name[i) + key < 195) { name[i] = (name[i] + key - 52); } else { name[i] = (name[i] + key); } } else { name[i] = name[i]; } } printf("cipher text: %s\n", name); }
[/dm_code_snippet]
My intention was to make the code work up to cycle that begins with 143 up to 194. I am surprised to see that up to key 18, the code works (z cipher text). But as I enter key 19, I get T as cipher text:
Instead of if (isalpha(name[i])), can isalpha be replaced by a CS50 function that will spot instead of letters, uppercase letters/lowercase letters. I tried with if (uppercase(name[i])), but did not work.
Reply
Yes isupper, and islower under I believe the ctype library
Use this next time, and remember, they give you clues on the pset itself.
Reply
https://edstem.org/us/courses/176/discussion/1004831?comment=2311979
[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.