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 › Do I need to create a function while converting keys to uppercase/lowercase for checking uniqueness
- This topic is empty.
-
AuthorPosts
-
February 23, 2022 at 1:07 pm #242
For converting to uppercase/lowercase for checking uniqueness of keys, do I need to create a function?
I have the following code in the main function that converts all keys to uppercase as part of checking the uniqueness of keys.
argv[1][j] = toupper(argv[1][j]);
Since keys are converted uppercase, there is no way to recover what the user actually entered as uppercase/lowercase.
Reply
No need to convert anything but can compare directly.
Try not to over complicate it. Keep it simple 🙂 Figure out how you can compare if ‘A’ and ‘a’ is the same letter.
I tried with the following code to check if the user enters uppercase or lowercase characters as part of plaintext:
[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”]
for (int i = 0; i < countstring; i++) //starts with first character of plaintext till the end { if (isupper s[i]) { s[i] = argv[1][s[i] - 65]; } else { s[i] = argv[1][s[i] - 97]; } }
[/dm_code_snippet]
Here is the full program:
[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> #include<stdlib.h> int main(int argc, string argv[]) { int counter = 0; if (argc != 2) { return 1; } string key = argv[1]; int t = strlen(key); if (t != 26) { printf("Usage: ./substitution key"); return 1; } while (counter < t) { if (!isalpha(argv[1][counter])) { printf("enter only alphabetic characters"); return 1; } else { counter = counter + 1; } } for (int i = 0; i < 25; i++) { for (int j = i; j < 25; j++) { argv[1][j] = toupper(argv[1][j]); printf("capital%c", argv[1][j]); if (argv[1][j] == argv[1][j + 1]) { printf("keys should be unique"); return 1; } } } string s = get_string("plaintext: "); int countstring = strlen(s);//count the number of characters entered by the user as plaintext printf("number of plaintext characters: %i", countstring); for (int i = 0; i < countstring; i++) //starts with first character of plaintext till the end { if (isupper s[i]) { s[i] = argv[1][s[i] - 65]; } else { s[i] = argv[1][s[i] - 97]; } } printf("ciphertext: %s", s); }
[/dm_code_snippet]
Here is the error screenshot:
Reply
if (isupper s[i])
The correct syntax is:
[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”]
isupper(s[i])
[/dm_code_snippet]
The error message tells you that isupper without the argument in parentheses will always evaluate to true. When you get an error message like that, even if you don’t understand the exact meaning, it points you in the direction where to look 🙂
Query
Thanks for the continued support. After revising, I fail to understand why else conditoin below not converting to lowercase of ciphertext.
[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”]
for (int i = 0; i < countstring; i++) //starts with first character of plaintext till the end { if (isupper (s[i])) { s[i] = argv[1][s[i] - 65]; } else { s[i] = argv[1][s[i] - 97]; } }
[/dm_code_snippet]
Reply
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
s[i] = argv[1][s[i] - 97];
[/dm_code_snippet]
This line finds the correct alphabetic position of a lowercase letter in plaintext and substitutes with the corresponding position in the key.
So if the letter in the key is uppercase, you are simply using that uppercase letter. You will need to make sure that the case of the cipher letter is the same case as the letter in plaintext.
[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.