Rather than identifying two similar formulas to represent the shift (one for uppercase keyword characters and one for lowercase characters), would it make sense to convert the keyword entered by the user to an all-lowercase-keyword before starting to encipher?

update: thank you for your answer! What do you mean by "ternary operator"?

I wondered whether it'd be "cleaner" to initially check whether the keyword entered was all-lowercase before encryption. If the keyword is not all-lowercase, the keyword could be converted to a 'all-lowercase-keyword' with the code below:

string key = argv[1];
string keyLower = key;

for (int j = 0; j < strlen(key); j++){
    keyLower[j] = tolower(key[j]);

The encryption would not need to have loops checking whether the jth character of the keyword is upper or lower-case.

Would it make the program more efficient and/or cleaner?

  • Absolutely, it's much cleaner and clearer too :)
    – Irene
    Oct 31 '15 at 21:49
  • Thank you, @Irene!
    – Chloe
    Oct 31 '15 at 23:07
  • please see the update section in the answer below!
    – kzidane
    Nov 1 '15 at 9:05
  • Oops, my bad. Thank you, Kareem!
    – Chloe
    Nov 1 '15 at 18:47

if I recall correctly, the reason most people had to do that was because encrypted uppercase plain chars had to be uppercase as well. similarly, encrypted lowercase plain chars had to be lowercase either.

if you follow this approach, you're not gonna be able to differentiate between the two, unless you cache this information somehow which I imagine wouldn't be that better than using two almost identical formulas.

do you think you can have something cleaner using the ternary operator?

update: given your question update, I think I misinterpreted your question as I thought you were talking about the plain text not the keyword.

recall that what you ultimately need from the keyword is the actual values of the keys, so instead of converting your keyword to lowercase first then deal with that later, you could do something like

for i = 0 to keyword length, increment i
    ensure keyword[i] is alphabetical
    keyword[i] = keyword[i] - (islower(keyword[i]) ? <some value> : <other value>)

that way, you wouldn't have to get any character in the keyword to map to a value in [0, 25] as you encrypt each plain char.

for more on the ternary operator, see http://www.cprogramming.com/reference/operators/ternary-operator.html!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .