2

I tried with this code:

let vowels: [Character] = ["a","e","i","o","u", "y"]

let replaced = String(myString.map {
     $0 == vowels.contains($0) ? "1" : "0"
 })

But I have the error: Binary operator '==' cannot be applied to operands of type 'Character' and 'Bool'

What is wrong?

cmii
  • 3,556
  • 8
  • 38
  • 69
  • The question is a little bit unclear - are you having problems with the algorithm or with the compile error? – Cristik Jul 09 '18 at 10:41
  • 1
    Let's translate: So `$0` is a `Character` object. `vowels.contains($0)` returns a `Bool`. How can `someCharacter == someBool` make sense to you? Now, let's back to the topic just do `vowels.contains($0) ? "1" : "0"`, because you just care about `vowels.contains($0)`. – Larme Jul 09 '18 at 10:48

5 Answers5

5

Just need to replace $0 == with return, you were comparing Character with Bool it makes no sense

let vowels: [Character] = ["a","e","i","o","u", "y"]

let replaced = String(myString.map {
     return vowels.contains($0) ? "1" : "0"
 })
Tj3n
  • 9,837
  • 2
  • 24
  • 35
1

replace all vowels in a String with asterisk symbol

let vowels: [Character] = ["a","e","i","o","u", "y"]
    var myString = "mahipal singh"
    let replaced = String(myString.map {
        vowels.contains($0) ? Character("*") : $0 // Replace * with your character you wanna to replace
    })
    print(replaced)
MAhipal Singh
  • 4,745
  • 1
  • 42
  • 57
  • Juste one question, why if I use `vowels.contains($0) ? $0 : Character("*")` to replace all character excepts *, all my characters are replaced by * ? – cmii Jul 09 '18 at 13:53
  • did you read comment of code ..you can set any character that you wanna to replace. i have set "*" as an example – MAhipal Singh Jul 09 '18 at 14:02
  • yes , this will replace all the character excepts * if you set vowels.contains($0) ? $0 : Character("*") – MAhipal Singh Jul 09 '18 at 14:39
  • It will not, it replaced ALL the characters with *, I don't understand why. – cmii Jul 09 '18 at 14:40
  • as per your condition vowels.contains($0) ? $0 : Character("*") , it check if vowels contain character of string it will print that vowel character and if vowels doesn't contain that character then it will replace with * on that place So the resultant string will be like this *a*i*a***i*** , it replace all the consonant with * and print all the vowel of string – MAhipal Singh Jul 09 '18 at 14:48
  • Swift is a type inferred language `Character` is not needed `vowels.contains($0) ? "*" : $0 ` – Leo Dabus Jul 09 '18 at 15:18
0

You can remove vowel from string like so:

string.remove(at: string.index(where: {vowels.contains($0)})!)
Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38
0
let vowels: [Character] = ["a","e","i","o","u","y","A","E","I","O","U", "Y"]
var replaced = String()
for char in myString {
  if !vowels.contains(char){
    replaced = "\(replaced)\(char)"
  }
}
Pranav Gupta
  • 741
  • 9
  • 10
-1

different way but it is still as you want

 let strs = "hello world"
    var str = String()
    let vowles = ["e","o"]
    //if you want the index also use  this (index,char) in strs.enumerated()
    //if the index is not important use this char in strs

    for (index,char) in strs.enumerated()
{


   var who = String(char)
    if vowles.contains(who){
        print(who)
        //replace or deleted by changing the value of who
         who = ""
        str = str + who
        print(str)

    }else{
        str = str + who

    }
}
Abd
  • 497
  • 4
  • 14