TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift ROT13 Func

Implement the ROT13 cipher with a func. Loop over and convert characters.
ROT13. This algorithm obscures text. It changes the letters in words to be shifted 13 places forward or backward. It is not encryption.
In Swift, we implement ROT13 in a func. We use the UnicodeScalar type to manipulate Characters by their numeric codes. We create a rotated string.
Func implementation. We introduce the rot13 func. This method receives a String and returns a String. It begins by creating an empty Character array.

Constants: We use some ASCII constants for letters. There values correspond to uppercase and lowercase letters in ASCII.

ASCII Table

Utf8: This accesses and returns the ASCII numeric values from the string's data. We cast these values to Ints.

Convert Int to Character

If, else: We test the character values with if and else. We use UnicodeScalar and shift letters 13 places depending on their values.

Return: We return a string based on the values appended to our Character array. We test rot13 in the rest of the program.

Swift program that implements rot13 func func rot13(value: String) -> String { // Empty character array. var result = [Character]() // Some ASCII constants. // A = 65 // M = 77 // Z = 90 // a = 97 // m = 109 // z = 122 let upperA = 65 let upperM = 77 let upperZ = 90 let lowerA = 97 let lowerM = 109 let lowerZ = 122 // Loop over utf8 values in string. for u in value.utf8 { let s = Int(u) var resultCharacter = Character(UnicodeScalar(s)) if s >= lowerA && s <= lowerZ { // Between a and z. if s >= lowerM { resultCharacter = Character(UnicodeScalar(s - 13)) } else { resultCharacter = Character(UnicodeScalar(s + 13)) } } else if s >= upperA && s <= upperZ { // Between A and Z. if s >= upperM { resultCharacter = Character(UnicodeScalar(s - 13)) } else { resultCharacter = Character(UnicodeScalar(s + 13)) } } // Append to Character array. result.append(resultCharacter) } // Return String. return String(result) } // Test the method. let input = "Do you have any cat pictures?" let result = rot13(input) let roundTrip = rot13(result) print(input) print(result) print(roundTrip) Output Do you have any cat pictures? Qb lbh unir nal png cvpgherf? Do you have any cat pictures?
Some notes. ROT13 is a terrible way to encrypt text. But this example helps us learn a programming language. We must access UnicodeScalars and manipulate character values in Swift.
A short review. A trick to manipulating strings in Swift is the use of UnicodeScalar. With this type we can shift characters based on their numeric values. The utf8 property also helps.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf