// Rotates each letter in a string of text by 13 places right. Rot13 // is a simple method of encoding text. Encoding entails shifting each // alphabetic character in a string of text right by thirteen places. // If the new character isn't alphabetic then the shift wraps round to // the start of the alphabet. // // Contributed by Derek (2010). // Minor alterations by Thomas Larsen (2010). option explicit print "Rot13 is a simple method of encoding text. Encoding entails shifting" print "each alphabetic character in a string of text right by thirteen" print "places. If the new character isn't alphabetic then the shift wraps" print "round to the start of the alphabet." print print "For example, A becomes N when shifted right by 13 places; M becomes Z" print "when shifted right by 13 places; N becomes A when shifted right by 13" print "places. So the Rot13 algorithm both \"encodes\" and \"decodes\" text." print // Main loop. do line input "Enter a string of text to convert (blank to exit): " string$ if string$ = "" exit print rot13$ (string$) loop // Rotate each character in a string of text by 13 places to the // right, wrapping around to the start of the alphabet if necessary. sub rot13$ (string$) local replace_ascii, character$, new_string$ // Deal with each character in the string. for character = 1 to len (string$) character$ = mid$ (string$, character, 1) // extract character replace_ascii = asc (character$) // get ASCII value of character // If the character is upper-case... if character$ >= "A" and character$ <= "Z" then // Rotate the character 13 places to the right. replace_ascii = replace_ascii + 13 // Wrap around to the start of the alphabet? if replace_ascii > asc ("Z") then replace_ascii = asc ("A") + ((replace_ascii - asc ("Z")) - 1) endif endif // If the character is lower-case... if character$ >= "a" and character$ <= "z" then // Rotate the character 13 places to the right. replace_ascii = replace_ascii + 13 // Wrap around to the start of the alphabet? if replace_ascii > asc ("z") then replace_ascii = asc ("a") + ((replace_ascii - asc ("z")) - 1) endif endif // Add the character to the end of the new string. new_string$ = new_string$ + chr$ (replace_ascii) next // Return the new string. return new_string$ end sub