0

I want to replace many characters in a string so it looks like the keyboard is moved a bit.

This is what the program executes: *qwertyuiopasdfghjklzxcvbnm mmmmmmmmmmmmmmmmmmmmmmmmmm *

Why does the program prioritize the last one always?

Can someone tell me why this is wrong and how to fix it

import UIKit

var str = "qwertyuiopasdfghjklzxcvbnm"
var replaced = str.replacingOccurrences(of: "m", with: "n").replacingOccurrences(of: "n", with: "b")    .replacingOccurrences(of: "b", with: "v").replacingOccurrences(of: "v", with: "c")    .replacingOccurrences(of: "c", with: "x").replacingOccurrences(of: "x", with: "z")    .replacingOccurrences(of: "z", with: "l").replacingOccurrences(of: "l", with: "k")    .replacingOccurrences(of: "k", with: "j").replacingOccurrences(of: "j", with: "h")    .replacingOccurrences(of: "h", with: "g").replacingOccurrences(of: "g", with: "g")    .replacingOccurrences(of: "g", with: "f").replacingOccurrences(of: "f", with: "d")    .replacingOccurrences(of: "d", with: "s").replacingOccurrences(of: "s", with: "a")    .replacingOccurrences(of: "a", with: "p").replacingOccurrences(of: "p", with: "o")    .replacingOccurrences(of: "o", with: "i").replacingOccurrences(of: "i", with: "u")    .replacingOccurrences(of: "u", with: "y").replacingOccurrences(of: "y", with: "t")    .replacingOccurrences(of: "t", with: "r").replacingOccurrences(of: "r", with: "e")    .replacingOccurrences(of: "e", with: "w").replacingOccurrences(of: "w", with: "q")    .replacingOccurrences(of: "q", with: "m")

print(str)
print(replaced)
6
  • 1
    Don't do it like that. Instead: var replaced = str.replacingOccurrences(of: "m", with: "n"); print(replaced); replaced = replaced.replacingOccurrences(of: "n", with: "b"); print(replaced"; replaced = ... and debug. You might be able to see what's happening. Commented May 21, 2021 at 10:38
  • What did you expect it to output? Commented May 21, 2021 at 10:44
  • var str = "mnbvcxz" var replaced = str.replacingOccurrences(of: "m", with: "n"); print(replaced); replaced = replaced.replacingOccurrences(of: "n", with: "b"); print(replaced); replaced = replaced.replacingOccurrences(of: "b", with: "v"); print(replaced); replaced = replaced.replacingOccurrences(of: "v", with: "c"); print(replaced); replaced = replaced.replacingOccurrences(of: "c", with: "x"); print(replaced); replaced = replaced.replacingOccurrences(of: "x", with: "z"); print(replaced); replaced = replaced.replacingOccurrences(of: "z", with: "m"); print(replaced) help Commented May 21, 2021 at 11:01
  • I did what you told me but now this executes: nnbvcxz bbbvcxz vvvvcxz cccccxz xxxxxxz zzzzzzz mmmmmmm mnbvcxz Commented May 21, 2021 at 11:03
  • nzvcxm - this is what I would like to print Commented May 21, 2021 at 11:06

2 Answers 2

1

As everybody else has said, the problem is that you are remapping the characters one at a time and you're doing it in the wrong order, so first you are replacing all the ms with n. Then you replace all the ns. But this includes the ones that were originally m. One way to fix this would be to go the other way. So replace m with n, then replace q with m and so on.

A better way is to do all the replacements at once.

First create a function to transform characters the way you want

func keyboardShift(c: Character) -> Character
{
    switch  c
    {
    case "q" : return "m"
    case "w" : return "q"
    case "e" : return "w"
    case "r" : return "e"
    case "t" : return "r"
    case "y" : return "t"
    case "u" : return "y"
    case "i" : return "u"
    case "o" : return "i"
    case "p" : return "o"
    case "a" : return "p"
    case "s" : return "a"
    case "d" : return "s"
    case "f" : return "d"
    case "g" : return "f"
    case "h" : return "g"
    case "j" : return "h"
    case "k" : return "j"
    case "l" : return "k"
    case "z" : return "l"
    case "x" : return "z"
    case "c" : return "x"
    case "v" : return "c"
    case "b" : return "v"
    case "n" : return "b"
    case "m" : return "n"
    default: return c
    }
}

Then use it as the argument to map().

let shiftArray = "the quick brown fox jumps over the lazy dog".map(keyboardShift)

map() returns Array<Character> so you need to create a new String from it.

shifted = String(shiftArray)
// shifted is 'rgw myuxj veiqb diz hynoa icwe rgw kplt sif'
Sign up to request clarification or add additional context in comments.

Comments

1

Your problem is that all the replacingOccurrences(of: with:) are being run sequentially, the result is qw becomes ww which becomes eee then rrrr etc

I gave this a go on playground so here's your solution:

What you need to do is break the string into an array of characters.

you want to shift the keyboard 1 to the right so take the last character of this array and stick it in the first element with .append()

here, this works :)

var str = "qwertyuiopasdfghjklzxcvbnm"
var strArray = Array(str) //turn str into an array
var last = str.last //get the last character of the array
strArray.popLast() //remove the last element/character of the array
strArray.insert(last!, at: 0) //insert the last character of the array in. the first element and push everything along 1

var string = ""
for char in strArray {
    string.append(char)
}
print(str) //input
print(strArray)//broken down string
print(string)//output

the line print(string) outputs mqwertyuiopasdfghjklzxcvbn which I believe is what you were going for :)

based on how much faster playground ran this snippet of code, I would suggest that this might be more efficient than replacing each character of the string in turn :P

1 Comment

I wrote the qwerty bit (at the top) the wrong way round, but the point is still made.... just in reverse order haha :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.