2

I am new to swift development. Actually it is a simple problem but I cannot figure it out. I have a String variable "name1" where I want to determine the position in the alphabet for each character and sum these positions. Therefore I loop through the string and use indexOf() for every character

let name1="myname"
let sumPositions=0

for index in 0...name1.characters.count-1{
sumPositions+="ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters.indexOf(name1.startIndex+index);
}

I get the error "Binary operator "+" cannot be applied to operands of type 'index' aka 'String.characterView.index' and 'Int'. This means I have to cast 'index' to 'Int' but I did not find a solution for this.

Kewitschka
  • 1,445
  • 1
  • 21
  • 35
  • Possible duplicate of [Apply a number to each letter in text swift2](http://stackoverflow.com/questions/35927148/apply-a-number-to-each-letter-in-text-swift2). (Or: [How do I cycle through the entire alphabet with Swift while assigning values?](http://stackoverflow.com/questions/28889172), depending on whether the former link is a duplicate of the latter) – dfrib Mar 21 '16 at 09:58

2 Answers2

1

Leo Dabus has a nice extension for this in this post

extension String {
    var letterValue: Int {
        return Array("abcdefghijklmnopqrstuvwxyz".characters).indexOf(Character(lowercaseString)) ?? 0
    }

    var wordValue: Int {
        // I prefer to use reduce
        return characters.reduce(0) { $0 + String($1).letterValue }
    }
}

let name1 = "myname"
print(name1.wordValue) // 65
Community
  • 1
  • 1
Eendje
  • 8,815
  • 1
  • 29
  • 31
  • Isn't it better we mark this one as a duplicate rather than duplicating an answer from another post? The Q is a duplicate of e.g. [the thread you link to](http://stackoverflow.com/questions/28889172/how-do-i-cycle-through-the-entire-alphabet-with-swift-while-assigning-values), or for the specific use case of word -> sum of letters, [this thread](http://stackoverflow.com/questions/35927148/apply-a-number-to-each-letter-in-text-swift2/35927473#35927473). – dfrib Mar 21 '16 at 09:57
  • Yea, except I've never looked into how to mark one as duplicate though. I see you've already done that so I'll just leave it be I guess? :) – Eendje Mar 21 '16 at 09:58
  • Ah, I see, no worries! You can mark (only a vote-as-duplicate: no dupehammer until 20k rep, I believe) a post a possible duplicate by pressing `close -> duplicate of` and paste the link to the thread you believe the one you're marking to be a duplicate of. – dfrib Mar 21 '16 at 10:00
  • You're right. Should I accept this answer anyway or what should I do? – Kewitschka Mar 21 '16 at 10:00
  • Seems easy enough, was too lazy and it's too much fun to solve these problems so I never really looked into it. Will try next time! @Kewitschka: Up to you! – Eendje Mar 21 '16 at 10:02
  • @Kewitschka Yeah sure accept the answer you believe helped you out. The duplicate marking is good mainly as it connects this thread with the other one (and also marks that we need no more answers in this thread). – dfrib Mar 21 '16 at 10:02
  • @Eendje Since this thread is already "alive" with two answer, I believe the OP can treat it as any regular thread (accept answer etc), but duplicate linking is good for connecting threads of very similar content. – dfrib Mar 21 '16 at 10:03
  • 2
    @dfri You're right, it is a duplicate. I should have posted my answer over there instead of here. I just closed this question with one of the links you've provided. Thanks. – Eric Aya Mar 21 '16 at 10:17
1

It can't work like that. My suggestion is that you create an array of letters from the alphabet, an array of letters from the name, then you use indexOf to find the letter index, you append all indices in an array then you sum the contents of the array:

let alphabetArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".lowercaseString.characters.map { String($0) } // ["a", "b", "c", "d", "e", "f", "g", ...]

let name1 = "myname"
let nameArray = name1.lowercaseString.characters.map { String($0) } // ["m", "y", "n", "a", "m", "e"]

var positions = [Int]()

for nameLetter in nameArray {
    if let index = alphabetArray.indexOf(nameLetter) {
        positions.append(index)
    }
}

print(positions) // [12, 24, 13, 0, 12, 4]

let sum = positions.reduce(0, combine: +) // 65
Eric Aya
  • 69,473
  • 35
  • 181
  • 253