whatiwunder whatiwunder - 1 month ago 9
Swift Question

swift reverse string and even numbers method

I'm doing some practice questions as i self teach myself swift that i was wonder if someone could assist me with.

Problem 1:

Create a swift app using the interface builder. You need to display one text box and one reverse string button. The user can enter any string in the textbox. Once the user clicks reverse string button, then display a label that shows entire reverse string.

Problem 2:

Create a swift app using the interface builder. You need to have one text box and submit button. Once user click submits button, display all the even number.

Input: 10

Ouput: 0,2,4,6,8,10

Input : 20

Ouput : 0,2,4,6,8,10,12,14,16,18,20

Answer

Reverse String

func reverse(_ input: String){
    let output = input.characters.reversed()
    print(output)
}

Even Numbers

func getEvens(_ input: Int){
     var output: String!
     for i in 0 ... input {
          if (i mod 2 == 0){
               output += i + ","
          }
     }
     output.remove(at: output.index(before: output.endIndex))
     print(output);
 }