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 › Clue what is wrong with my isalpha use
- This topic is empty.
-
AuthorPosts
-
February 11, 2022 at 6:12 am #211
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
#include #include #include #include #include int main(int argc, string argv[]) { int counter = 0; if (argc != 2) { return 1; } else { printf("enter plainkey: "); } string key = argv[1]; int t = strlen(key); if(t != 26) { return 1; } while(counter < t) { isdigit (argv[1][counter]); return 1; counter = counter + 1; } string s = get_string("plaintext: "); int countstring = strlen(s);//count the number of characters entered by the user as plaintext for(int i = 0; i <= countstring; i++)//starts with first character of plaintext till the end { if (isalpha s[i])//if character is alphabetic { int s[i]= argv[1][i];//replace index value of alphabetic character with the same position of key array } else { s[i] = s[i];//if not alphabetic, then the entered character remains the same } } printf("ciphertext: %s",s); }
[/dm_code_snippet]
What I intend to do is to check one-by-one if entered character as part of plaintext is alphabetic or not. If not alphabetic, then the output in ciphertext remains the same. If alphabetic, then the ASCII value of that alphabet is replaced with the key array value in that position.
If I have failed to explain, kindly let me know so that I try to rephrase.
Reply
Details matter 🙂
[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"]
if (isalpha s[i])
[/dm_code_snippet] Here you forgot the () to use s[i] as argument to isalpha: [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"]
if (isalpha(s[i]))
[/dm_code_snippet]
As I understand from the other comments here, you have already moved on to use isupper and islower here instead, good choice.
Another thing is that in this pset you want to make sure that the key is all alphabetic and has no duplicates and has exactly 26 characters. You do check for 26 characters but you are not checking for duplicates and alphabetic.
Unlike in caesar we don’t care if a character is a digit. There are other characters than digits that are not alphabetic. For example @#$%^& and more 🙂 So maybe isalpha can be useful afterall, just not where you used it before 🙂
I think that isalpha should not be used here. Use isupper and islower directly because isalpha will add clutter to your code.
If you’ll see at Substitution > Background, you’ll see that code should output small letter if it’s lowercase, and big letter if it’s uppercase.
And small hint from me: make function for encryption that will take letter and key.
The program should check if input meets requirements:
-
Only one input (you can use argc for that)
-
Check if key is made only from letters and is {some number} long
If there’s different number of inputs, then output “Usage: ./substitution key”
If there’s good number of input (1), check if key is correct. If key is not correct, output “Key must contain 26 characters.” If key is correct, do encryption (plaintext > ciphertext)
Edit: I see that you added this, but you need to add printf statements to make it work the way it intended to work. It might help you with fixing code. If program is stuck on some point, try to find the bug
Good luck ????
[learn_press_profile]
-
-
AuthorPosts
- You must be logged in to reply to this topic.