1

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 ǝɯoɥ upside down.

I've tried looking for in the book to get started, but nothing.

Jerry Lynch
  • 83
  • 2
  • 8
  • Can you talk a little more about the use case for this? Is this for aesthetics? Where would this be seen? On a web page? – sheriffderek Feb 16 '13 at 18:25
  • 1
    Related to: http://stackoverflow.com/questions/2995340/how-does-uwop-episdn-text-work – dyoo Feb 16 '13 at 19:08
  • not for a website. its for drscheme. I tagged scheme and racket. guess i should have said this earlier. – Jerry Lynch Feb 16 '13 at 19:10
  • Note: if the requirement is changed so the output doesn't have to be a string, then we can use a combination of `text` and `rotate` from Racket's `2htdp/image` library. http://docs.racket-lang.org/teachpack/2htdpimage.html – dyoo Feb 17 '13 at 00:15

2 Answers2

4

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 . #\) (#\C . #\Ɔ) (#\D . #\◖)
  (#\E . #\Ǝ) (#\F . #\Ⅎ) (#\G . #\⅁) (#\H . #\H) (#\I . #\I) (#\J . #\s)
  (#\K . #\⋊) (#\L . #\˥) (#\M . #\W) (#\N . #\N) (#\O . #\O) (#\P . #\Ԁ)
  (#\Q . #\Ό) (#\R . #\ᴚ) (#\S . #\S) (#\T . #\⊥) (#\U . #\∩) (#\V . #\Λ)
  (#\W . #\M) (#\X . #\X) (#\Y . #\⅄) (#\Z . #\Z) (#\0 . #\0) (#\1 . #\Ɩ)
  (#\2 . #\ᄅ) (#\3 . #\Ɛ) (#\4 . #\ㄣ) (#\5 . #\ϛ) (#\6 . #\9) (#\7 . #\ㄥ)
  (#\8 . #\8) (#\9 . #\6)))

(define (flip-string str)
  (list->string
   (map (lambda (c)
          (hash-ref upside-map c (const c)))
        (reverse (string->list str)))))

For example:

(flip-string "Hello World")
=> "pןɹoM oןןǝH"

For reference, I used this conversion table taken from Wikipedia.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Thanks Oscar. It desn't seem to work though. I am using Beginning Student with List Abbreviations. Maybe thats the reason. – Jerry Lynch Feb 16 '13 at 23:12
  • @JerryLynch yes, that's probably the reason. Then you'll have to do this: create a `cond` in the `lambda` (instead of `hash-ref`) and write a case for each possible character. Like this: `(cond ((char=? c #\a) #\ɐ) ...lots of conditions... (else c))`. That'll do the trick. – Óscar López Feb 16 '13 at 23:21
  • @JerryLynch I don't think that's in the scope of this question (and doesn't seem to work, either). Anyway, in Racket exists the procedure `char-downcase` that converts a single character to lowercase, and it's a simple matter calling it over each of the elements of a list. – Óscar López Feb 16 '13 at 23:50
  • Dunno if it was supported when this was written, but `#\u10412` should be `#\U10412` (note the upper case U to get the full range of Unicode code points; u is limited to just the BMP) – Shawn Jul 24 '23 at 00:55
  • @Shawn thanks! I've updated my answer, your comment is correct but it seems there's no longer a restriction in the number of bits for representing a character so I removed that part. – Óscar López Jul 24 '23 at 21:09
0

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

Leo
  • 63
  • 1
  • 14