matteoDominoni
matteoDominoni

Reputation: 35

How to compare each letters in an array with all the letters in another array? - Swift

i have 2 different arrays called: criptedChar and alphabet. I need to check the first character in criptedChar (so "criptedchar[0]) and check a correspondence in alphabet. For exemple: criptedChar // ["d","e","c","b"] alphabet // ["a","b","c" and so on] I want to take d from criptedChar[0] and check if there's a "d" in all alphabet and then save the position of "d" in the second array. I also need to increment the number inside the parenthesis of criptedChar. I'll take the number from the user. Can you please help me? Thank you!

func decript() {
    var criptedText = incriptedText.text!   //get text from uiTextField
    var criptedChar = Array<Character>(criptedText.characters)  //from text to char & all in array :D
    var alfabeto: Array<Character> = ["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 capacityCriptedCharArray = criptedChar.capacity

    for (var i = 0; i < 26; i++) {
        if criptedChar[0] == alfabeto[i] {
            decriptedText.text = decriptedText.text! + "\(newLettersFromSecondViewController[i])"
        }
    }


    for (var i = 0; i < 26; i++) {
        if criptedChar[1] == alfabeto[i] {
            decriptedText.text = decriptedText.text! + "\(newLettersFromSecondViewController[i])"
        }
    }

    for (var i = 0; i < 26; i++) {
        if criptedChar[2] == alfabeto[i] {
            decriptedText.text = decriptedText.text! + "\(newLettersFromSecondViewController[i])"
        }
    }   
}

This code works, but it's dumb and i have no control of the user input

Upvotes: 1

Views: 2511

Answers (2)

Alain T.
Alain T.

Reputation: 42143

Here are examples of simple cypher logic that you can probably adapt to your application:

let readableText = "the quick brown fox jumped over the lazy dog"

// letters: letters in readable text that will be encoded
// cypher : corresponding encoded letters
//
// note: letters and cypher must have the same number of elements
let letters:[Character] = ["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"]
let cypher:[Character]  = ["o","p","q","r","a","b","c","d","e","f","g","h","i","u","v","w","x","y","z","j","k","l","m","n","s","t"]

// build a mapping disctionary from readable to encoded
var encode:[Character:Character] = [:]
for (index, letter) in letters.enumerate() { encode[letter] = cypher[index] } 

// encrypt the readble text  gives: "jda xkeqg pyvmu bvn fkiwar vlay jda hots rvc"
let cryptedText = String(readableText.characters.map({ encode[$0] ?? $0 })) 


// build a mapping disctionary from encoded to readable
var decode:[Character:Character] = [:]
for (index, letter) in cypher.enumerate() { decode[letter] = letters[index] }

// decrypted the encrypted text  gives: "the quick brown fox jumped over the lazy dog"
let decryptedText = String(cryptedText.characters.map({ decode[$0] ?? $0 })) 

Upvotes: 0

Martin R
Martin R

Reputation: 539795

If I understand your question correctly, you are looking for something like this (explanations inline):

// Start with your crypted text, and an empty string for the result:
let cryptedText = "mifpyx"
var decryptedText = ""

// Two character arrays (of equal length):
let alphabet   = Array("abcdefghijklmnopqrstuvwxyz".characters)
let newLetters = Array("ghijklmnopqrstuvwxyzabcdef".characters)

// For each character in the input string:
for c in cryptedText.characters {
    // Check if `c` is contained in the `alphabet` array:
    if let index = alphabet.indexOf(c) {
        // Yes, it is, at position `index`!
        // Append corresponding character from second array to result:
        decryptedText.append(newLetters[index])
    }
}

print(decryptedText) // solved

Alternatively, you can create a lookup-dictionary from the two arrays:

var mapping = [ Character : Character ]()
zip(alphabet, newLetters).forEach {
    mapping[$0] = $1
}

and then map each character from the input through that dictionary:

let decryptedText = Array(cryptedText.characters
        .map { mapping[$0] }
        .flatMap { $0 }
    )

(Here flatMap is used to filter-out the nils from characters which are not present in the input array.)

Upvotes: 3

Related Questions