Questions for readLine() / Optionals

HI All,
I would need some Help to Understand what’s wrong.

See code

var Input:String? = readLine()
if let ActualInput = Input {
print(“English:(Input)”)
}
else {
print(“no solution”)
}
var Output:String? = String(Input!.reversed()).
if let ActualOutput = Output{
print (“Alien:(Output)”)
}
else {
print (“no solution”)
}

Question 1:
if input is Hello
results are :
English:Optional(“Hello”)
Alien:Optional(“olleH”)
But Why are Optionals before (“Hello”) there???

Question 2:
if Input is empty --> Result are:
English:Optional("")
Alien:Optional("")
why “Else” Condition doesn’t work???

Question 3:
see line: " var Output:String? = String(Input!.reversed())"
It is working only if I Add “!” after Input. but Theoretically it it already wrapped up ? isn’t it?

Thank you !
J.

Question 1:
if input is Hello
results are :
English:Optional(“Hello”)
Alien:Optional(“olleH”)
But Why are Optionals before (“Hello”) there???

Because for the English one you are printing out Input, which is an Optional String. For the Alien one, you are printing out Output, which is also an Optional String. You should be printing out ActualInput and ActualOutput instead.

(And you really should be naming your variables like input, actualInput, output, actualOutput, but that’s a different issue.)

Question 2:
if Input is empty → Result are:
English:Optional(“”)
Alien:Optional(“”)
why “Else” Condition doesn’t work???

An empty string is not the same as a nil string. readLine() is returning an empty string but you are checking for a nil string.

Question 3:
see line: " var Output:String? = String(Input!.reversed())"
It is working only if I Add “!” after Input. but Theoretically it it already wrapped up ? isn’t it?

It is, so you have to unwrap it to get at the string value inside the Optional in order to reverse it.

Thank you for your answer ! first code ever I’ve ever made:
I think I solved it. :slight_smile:

/* Extra-Terrestrials

You meet a group of aliens, and their language is just like English except that they say every word backwards.
How will you learn to communicate with them?

Task:
Take a word in English that you would like to say, and turn it into language that these aliens will understand.

Input Format:
A string of a word in English.

Output Format:
A string of the reversed word that represents the original word translated into alien language.

Sample Input:
howdy

Sample Output:
ydwoh
*/

func In(Input:String?) {
if let unwraped = Input , Input != nil && Input != “”{
print(“the English word to translate is : (unwraped)”)
let Alien = String(unwraped.reversed())
print(“it means in Alien :(Alien)”)
} else if Input == “”{
print(" Add text to translate")
}else {

}

}
In(Input:readLine())

if let unwraped = Input , Input != nil && Input != “”{

You don’t need to test if Input != nil since you already unwrapped the Optional with let unwraped = Input. If Input is nil then your code will jump down to the next condition when you try to unwrap it and won’t ever get to the Input != nil part anyway.

And it’s better to test for an empty string using isEmpty than != "". So let’s rewrite that as:

if let unwraped = Input, !unwraped.isEmpty {