0

FINAL OBJECTIVE: Turn integers into written long hand.

I have seen some discussions on this which are over my head. I have come across functions which happily break up a string and print out the characters but capturing them in an array seems impossible.

It seems individual characters can be accessed by subscript but they can't be operated on.

The following will print out 1,2,9,.,5,0 if I remove the commenting out but when I run through the if loop I get written number strings but in the wrong sequence.

let sentence = "129.50"
for (character) in sentence {
  //  print(character)
        if character == "0" {
        print("zero")
    }
        if character == "1" {
            print("one")
    }
        if character == "2" {
        print("two")
    }

                etc etc

I have also tried to access the indexing function via a function and although it prints out in full every time it always crashes at the end.

func speakNum(_ num:Int) {
    let strgNum = String(num)
    for t in 0...strgNum.count {
        let index = strgNum.index(strgNum.startIndex, offsetBy:t)
        //strgnum.index(strgNum.startIndex, offsetBy:t)
      print(String(strgNum[index]))
    }
}

Any help appreciated.

5
  • 1
    for t in 0..<strgNum.count, not for t in 0...strgNum.count. Commented Sep 3, 2018 at 0:46
  • 1
    What output do you actually get with your first set of code? BTW - Use a switch instead of all of those if statements. Or at least use if else since only one can be true in a given iteration. Commented Sep 3, 2018 at 0:48
  • For your example, what do you want to get back? Commented Sep 3, 2018 at 0:54
  • Please add the actual output of your code and show your complete code sample for it. Commented Sep 3, 2018 at 1:01
  • Maddy: Ach... of course 0..<strgNum.count - thank you. The output I get for all those if statements is: one two fife six seven eight niner decimal zero Commented Sep 3, 2018 at 7:47

3 Answers 3

1

This is an excellent time for you to learn TDD. Test Driven Development. Start off with a simple case, the simplest you can think of...

assert(writtenOut("1") == "one")

Get the above working then add another test:

assert(writtenOut("1") == "one")
assert(writtenOut("2") == "two")

Do the above for all the numbers and the decimal. You should also handle error cases:

assert(writtenOut("d") == "")

Then try for something more complex:

assert(writtenOut("12") == "one two") // or do you want "twelve" in this case?

You can do this yourself, start start small and work your way up. By the time you are done, you will have a working function and a whole bunch of tests that prove it works.

Sign up to request clarification or add additional context in comments.

4 Comments

While this may be good advice, this answer doesn't actually make any attempt to answer the question being asked.
@rmaddy - "Teach a man to fish...", you are right Daniel hasn't attempted to answer just this question but also a few future ones...
@CRD I'm all for the "teach a man to fish..." ideas but SO is a Q&A site. Post an answer that solves the question being asked. Then, if the mood strikes, add some helpful additional details. Imagine how useless this site would be if this answer was posted to every question on the site.
@rmaddy - But isn't it a question of balance? For some simple/beginner questions just provide a rod and instructions, for the tough/obscure provide a detailed answer, etc. You see cases where people ask question after question using SO in place of thought/effort - sure it bulks up SO but does it really help the questioner (or their future customers)? Sadly people do sometimes get downvotrs for helping/suggesting people think, doesn't help SO. I think Daniel hit the right balance here in helping the questioner, YMMV of course. Have a nice day!
1

Try this

    let str = "129.50"
    let array = Array(str)
    print(array)

prints ["1", "2", "9", ".", "5", "0"]

Comments

0

Thanks to all for feedback, I have ended up with this which seems a bit cumbersome but does work:

func radio(_ MHz:Double){
let sentence = String(MHz)
    for (character) in sentence {
            if character == "0" {
                print("zero", terminator:" ")
        }
            if character == "1" {
                print("one", terminator:" ")
        }
            if character == "2" {
                print("two", terminator:" ")
        }
            if character == "3" {
                print("tree", terminator:" ")
        }
            if character == "4" {
                print("fower", terminator:" ")
        }
            if character == "5" {
                print("fife", terminator:" ")
        }
            if character == "6" {
                print("six", terminator:" ")
        }
            if character == "7" {
                print("seven", terminator:" ")
        }
            if character == "8" {
                print("eight", terminator:" ")
        }
            if character == "9" {
                print("niner", terminator:" ")
        }
            if character == "." {
                print("decimal", terminator:" ")
        }
    }
    print()
    print()
}

Thus radio(118.65) yields

one one eight decimal niner fife

4 Comments

Why use this huge stack of ifs if you can use switch? Or even Dictionary where keys are digits and values are digit printouts strings?
radio(117.9) yields the default case 5 times in a row - I have no idea why.
Can't usefully post my switch case code in comments but it keeps going to the default case - are you sure the for in loop for strings is compatible with switch? Seems a crazy question but from a beginners POV there appear to be many inconsistencies in Swift though I have no doubt this is just due to ignorance on my part.
This is the same code as in your question. How does this solve your original issue?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.