rot13

Where is my implementation of rot13 in JavaScript going wrong?

谁说胖子不能爱 提交于 2019-11-26 17:43:23
Code in question with syntax highlighting here: via Friendpaste rot13.js: ERRONEOUS <script> String.prototype.rot13 = rot13 = function(s) { return (s = (s) ? s : this).split('').map(function(_) { if (!_.match(/[A-Za-z]/)) return _; c = Math.floor(_.charCodeAt(0) / 97); k = (_.toLowerCase().charCodeAt(0) - 96) % 26 + 13; return String.fromCharCode(k + ((c == 0) ? 64 : 96)); }).join(''); }; </script> As you can see, using quite literally a single line to attach a method to the String object a la prototyping, I'm having a map() method that I previously set up (I know for sure that that code works

ROT-13 function in java?

拈花ヽ惹草 提交于 2019-11-26 11:37:39
问题 Is there already a rot13() and unrot13() implementation as part of one of the standard Java libraries? Or do I have to write it myself and \"reinvent the wheel\"? It might look something like this: int rot13 ( int c ) { if ( (c >= \'A\') && (c <= \'Z\') ) c=(((c-\'A\')+13)%26)+\'A\'; if ( (c >= \'a\') && (c <= \'z\') ) c=(((c-\'a\')+13)%26)+\'a\'; return c; } 回答1: I don't think it's part of Java by default, but here's an example of how you can implement it; public class Rot13 { public static

Where is my implementation of rot13 in JavaScript going wrong?

一笑奈何 提交于 2019-11-26 05:34:21
问题 Code in question with syntax highlighting here: via Friendpaste rot13.js: ERRONEOUS <script> String.prototype.rot13 = rot13 = function(s) { return (s = (s) ? s : this).split(\'\').map(function(_) { if (!_.match(/[A-Za-z]/)) return _; c = Math.floor(_.charCodeAt(0) / 97); k = (_.toLowerCase().charCodeAt(0) - 96) % 26 + 13; return String.fromCharCode(k + ((c == 0) ? 64 : 96)); }).join(\'\'); }; </script> As you can see, using quite literally a single line to attach a method to the String object