checking if input has the same characters
Hey guys, I'm working on a script that ciphers your input, but before you put your input in, you have to provide a key; ./substitution VcHpRZGJnTLSkFBDqWAXeUYMOI. One of the checks is to make sure the key doesn't have the same letter. I had to use someone else's code to check this. I couldn't figure it out. There's one line in there I can't understand; "if(usedChar[substitution[i] - 'A'] == true)". I don't understand how the minus capital "A" works? Any clarity on this would be appreciated. Thank you! int main(int argc, string argv[]) { // Making sure two aruments are inputed if (argc != 2) { printf("Usage: %s key\n", argv[0]); return 1; } // Making sure the input has 26 characters else if (strlen(argv[1]) != 26) { printf("Key must contain 26 characters.\n"); return 1; } string substitution = argv[1]; char key[26]; bool usedChar[26]; for (int i = 0; i < 26; i++) { usedChar[i] = false; } for (int i = 0; i < 26; i++) { if (isalpha(substitution[i])) { if (isupper(substitution[i])) { if(usedChar[substitution[i] - 'A'] == true) { printf("Duplicate key: %c\n", substitution[i]); return 1; } else { usedChar[substitution[i] - 'A'] = true; key[i] = substitution[i] -'A'; } } else { if (islower(substitution[i])) { if(usedChar[substitution[i] - 'a'] == true) { printf("Duplicate key: %c\n", substitution[i]); return 1; } else { usedChar[substitution[i] - 'a'] = true; key[i] = subst