I'm trying to find the position of the letters that make up a string. For example, I need to know the positions of the letter "c" in the word "character" for further calculation.
I tried
for letters in Array("character".characters) {
if "c".contains(String(letters)) {
if let i = Array("character".characters).index(of: letters) {
print(i)
}
} else { print("wrong letter") }
}
// Console:
0
wrong letter
wrong letter
wrong letter
wrong letter
0
wrong letter
wrong letter
wrong letter
All I get from the console are two zeros; it's only giving me the index of the first c in "character" but not the second c. The fact that it prints out "wrong letter" means the loop is running correctly; it even recognise the position of the second c, it's just not giving the correct index.