Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

References: Sections 2.1 (Strings) and 2.5 (Collections) of \" Application Devel

ID: 3745667 • Letter: R

Question


References:  Sections 2.1 (Strings) and 2.5 (Collections) of "Application Development with Swift"

You are given a dictionary crypt of type [String:String] which has values for all lowercase letters. The crypt dictionary represents a way to encode a message. For example, if crypt["h"] = "@" and crypt["i"] = "!" the encoded version of the message "hi" will be "@!".

Part 1: Write code that would take any string containing only lower case letters and spaces and encode it using the crypt dictionary (non-lower case letters can be encoded into themselves).

Use the following crypt dictionary to test your code with the string message "hello swift":

=================
var crypt = [
    "a" : "$",
    "b" : "&",
    "c" : "",
    "d" : "*",
    "e" : "%",
    "f" : "",
    "g" : ")",
    "h" : "@",
    "i" : "!",
    "j" : ">",
    "k" : "<",
    "l" : "{",
    "m" : "}",
    "n" : "?",
    "o" : "/",
    "p" : "",
    "q" : "-",
    "r" : "",
    "s" : "~",
    "t" : "",
    "u" : ".",
    "v" : "[",
    "w" : "]",
    "x" : "|",
    "y" : ":",
    "z" : "_",
    " ": ""
]

Sample input:  crypto swift

Expected Output: :/~]!

Part 2: Write a function that takes a string and returns its encoded version using the crypt dictionary. Test your function with few sample strings including the sample input above.

Deliverables:  Submit (1) a copy of your code for Part 2 (be sure to put your name and indicate which part of the assignment you completed), (2) screen shot of at least two test cases showing your solution meets problem specs above.

 I will pls if you help. thanks

Explanation / Answer

PART 1:

import Swift

var crypt:[String:String]=[

"a" : "$",
"b" : "&",
"c" : "",
"d" : "*",
"e" : "%",
"f" : "",
"g" : ")",
"h" : "@",
"i" : "!",
"j" : ">",
"k" : "<",
"l" : "{",
"m" : "}",
"n" : "?",
"o" : "/",
"p" : "",
"q" : "-",
"r" : "",
"s" : "~",
"t" : "",
"u" : ".",
"v" : "[",
"w" : "]",

    "x" : "|",
"y" : ":",
"z" : "_",
" ": ""

]      

var str1: String

var sr2: String

print("Please enter a string to be encoded")

let str1=readLine()

str1=str1.lowercased()

let str2=""

for ch in str1

{

for (key , value) in crypt {   'looks for the dictionary

if ch==(key)     'if it matches a character in keys

str2=str2 + (value)    'then it appends its matching encode to the output string str2

}

}

PART 2:

func encodefun(str1 : String , crypt : [String : String]) -> String {

str1=str1.lowercased()

let str2:String=""

for ch in str1   'for each character from string str1

{

for (key , value) in crypt {   'looks for the dictionary

if ch==(key)     'if it matches a character in keys

str2=str2 + (value)    'then it appends its matching encode to the resultant string str2

}

}

return str2   'encoded string is returned

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote