+ 2
Code algorithm and optimization
Is a dictionary object like a hashtable the best way to do this? https://code.sololearn.com/cMGCFjL8JWaL/?ref=app
5 Réponses
+ 1
Sorry, I couldn't immediately decipher your intended algorithm. It is a massive violation of the KISS principle, so I would say no, it is not the best way to do this. Go with simpler, like these lines that do direct calculation:
string s = Console.ReadLine().ToLower();
//convert alpha chars to backward alphabet
foreach (char ch in s) {
Console.Write(Char.IsLetter(ch) ? (char)('z'-ch+'a') : ch);
}
+ 2
Brian If you want to customise this for random assignment then I think that what I did is correct
+ 1
Brian What if some of the keys are uppercase and some lowercase....???
+ 1
In that case, yes, a lookup table is better. For my taste, the KISS approach would use a simple array.
char[] t = "qwertyuiopasdfghjklzxcvbnm".ToCharArray();
string s = Console.ReadLine().ToLower();
//convert alpha chars to jumbled alphabet
foreach (char ch in s) {
Console.Write(Char.IsLetter(ch) ? t[ch-'a'] : ch);
}
0
Sanjay Kamath, just like your original code, string s is made all lowercase by use of the ToLower() method in the input line.