Upside down text

后端 未结 2 1134
时光说笑
时光说笑 2021-01-26 19:24

How would you design a program that will take in a string of lower case letters and produce the string upside down?

so if I type in home

i get

相关标签:
2条回答
  • 2021-01-26 20:03

    first of all your website must support Unicode, Unicode consists thousands of characters, the first 127 of Unicode are ASCII. It is possible to create text that appears to be upside down by converting character by character to a Unicode sign that looks like the upside down version of the character, for example to convert "6" you can use "9" but the flipped version of "f" is "ɟ", which is a Latin character with Unicode number 607 (hex code 025F)

    technically, you need two text area boxes, one for the original text and the other one for the flipped text, also you need a Javascript, use the onkeyup Javascript hook in the first text box to call an upsideDownText() function each time a key is released like this:

    <textarea rows="5" cols="70" id="src" onkeyup="upsideDownText()"></textarea>
    

    then do the text processing in the upsideDownText() Javascript function like this:

    <script type="text/javascript">
    function upsideDownText() {
      var srcText = document.getElementById( 'src' ).value.toLowerCase();
      var out = '';
      for( var i = srcText.length - 1; i >= 0; --i ) {
        var ch = srcText.charAt( i );
        if( ch == 'a' ) {
          out += '\u0250' }
        } else if( ch == 'b' ) {
          out += 'q' }
        } else if( ch == 'c' ) {
          out += '\u0254'
        // etc....
        } else {
          out += ch
        }
      }
      document.getElementById( 'dest' ).value = out;
    }
    </script>
    

    get the content of the text box identified by id="src" and convert the string to lowercase using the toLowerCase() method. Then loop through the string, character by character, starting from the end of the string. A big if-then-else-if block handles the character conversion. finally push the converted string into the text box identified by id="dest", which is the lower text box.

    you can find the full list of how doing this step by step from Source twiki.org

    0 讨论(0)
  • 2021-01-26 20:05

    Try this, a bit of a brute-force approach but works quite well for uppercase, lowercase and number characters - all other characters are presented just as they come:

    (define upside-map '#hash(
      (#\a . #\ɐ) (#\b . #\q) (#\c . #\ɔ) (#\d . #\p) (#\e . #\ǝ) (#\f . #\ɟ)
      (#\g . #\ƃ) (#\h . #\ɥ) (#\i . #\ı) (#\j . #\ɾ) (#\k . #\ʞ) (#\l . #\ן)
      (#\m . #\ɯ) (#\n . #\u) (#\o . #\o) (#\p . #\d) (#\q . #\b) (#\r . #\ɹ)
      (#\s . #\s) (#\t . #\ʇ) (#\u . #\n) (#\v . #\ʌ) (#\w . #\ʍ) (#\x . #\x)
      (#\y . #\ʎ) (#\z . #\z) (#\A . #\∀) (#\B . #\                                                                    
    0 讨论(0)
提交回复
热议问题