How to check if string contains a certain character? - swift

I've been trying to find this but I can't. I have the code:
func ayylmao(vorno: String){
if (vorno.(WHATEVER THE FUNCTION FOR FINDING A STRING GOES HERE)("a", "e", "i", "o", "u"))
{
print("Input Includes vowels")
}
}
but right at the if statement I can't find anything to check if the characters are in the string.

Like this:
let s = "hello"
let ok = s.characters.contains {"aeiou".characters.contains($0)} // true

I suggest two implementations:
func numberOfVowelsIn(_ string: String) -> Int {
let vowels: [Character] = ["a", "e", "i", "o", "u", "y", "A", "E", "I", "O", "U", "Y"]
return string.reduce(0, { $0 + (vowels.contains($1) ? 1 : 0) })
}
numberOfVowelsIn("hello my friend") //returns 5.
... and the second one with this code snippet hereafter to reach your goal:
let isVowel: (Character) -> Bool = { "aeiouyAEIOUY".contains($0) }
isVowel("B") //returns 'false'.
isVowel("a") //returns 'true'.
isVowel("U") //returns 'true'.

Related

String sort using CharacterSet order

I am trying to alphabetically sort an array of non-English strings which contain a number of special Unicode characters. I can create a CharacterSet sequence which contains the desired lexicographic sort order.
Is there an approach in Swift5 to performing this type of customized sort?
I believe I saw such a function some years back, but a pretty exhaustive search today failed to turn anything up.
Any pointers would be appreciated!
As a simple implementation of matt's cosorting comment:
// You have `t` twice in your string; I've removed the first one.
let alphabet = "ꜢjiyꜤwbpfmnRrlhḥḫẖzsšqkgtṯdḏ "
// Map characters to their location in the string as integers
let order = Dictionary(uniqueKeysWithValues: zip(alphabet, 0...))
// Make the alphabet backwards as a test string
let string = alphabet.reversed()
// This sorts unknown characters at the end. Or you could throw instead.
let sorted = string.sorted { order[$0] ?? .max < order[$1] ?? .max }
print(sorted)
Rather than building your own “non-English” sorting, you might consider localized comparison. E.g.:
let strings = ["a", "á", "ä", "b", "c", "d", "e", "é", "f", "r", "s", "ß", "t"]
let result1 = strings.sorted()
print(result1) // ["a", "b", "c", "d", "e", "f", "r", "s", "t", "ß", "á", "ä", "é"]
let result2 = strings.sorted {
$0.localizedCaseInsensitiveCompare($1) == .orderedAscending
}
print(result2) // ["a", "á", "ä", "b", "c", "d", "e", "é", "f", "r", "s", "ß", "t"]
let locale = Locale(identifier: "sv")
let result3 = strings.sorted {
$0.compare($1, options: .caseInsensitive, locale: locale) == .orderedAscending
}
print(result3) // ["a", "á", "b", "c", "d", "e", "é", "f", "r", "s", "ß", "t", "ä"]
And a non-Latin example:
let strings = ["あ", "か", "さ", "た", "い", "き", "し", "ち", "う", "く", "す", "つ", "ア", "カ", "サ", "タ", "イ", "キ", "シ", "チ", "ウ", "ク", "ス", "ツ", "が", "ぎ"]
let result4 = strings.sorted {
$0.localizedCaseInsensitiveCompare($1) == .orderedAscending
}
print(result4) // ["あ", "ア", "い", "イ", "う", "ウ", "か", "カ", "が", "き", "キ", "ぎ", "く", "ク", "さ", "サ", "し", "シ", "す", "ス", "た", "タ", "ち", "チ", "つ", "ツ"]

"Self" working in switch statement, but not If statement in Swift [duplicate]

This question already has answers here:
How to do if pattern matching with multiple cases?
(4 answers)
Closed 3 years ago.
this code works
extension Character {
func isVowel() -> Bool {
switch self {
case "a", "e", "i", "o", "u", "A", "E", "I", "O", "U":
return true
default:
return false
}
}
}
but when I use an if statement it gives "cannot convert type "String" to Bool
extension Character {
func isVowel() -> Bool {
if "a", "e", "i", "o", "u" {
return true
} else {
return false
}
}
}//does not work
I have tried adding "self" and self.asciiValue
how to properly convert the switch statement into if statement? sorry, still a beginner lol
The if statement should look like this:
if self == "a" || self == "e" || self == "i" || self == "o" || self == "u" {
return true
} else {
return false
}
Another way to do this would be:
func isVowel() -> Bool {
return ["a", "e", "i", "o", "u"].contains(self)
// or even shorter (by Leo Dabus):
return "aeiou".contains(self)
}

How to sort a multidimensional character array alphabetically Swift

I am trying to sort a [[Character]] array that has random characters put in to it so that it is all alphabetical. ex output for right now:
["H", "P", "C"]
["F", "K", "V"]
["J", "Y", "B"]
I need it to be like this
["A", "B", "C"]
["D", "E", "F"]
["G", "H", "I"]
Any Ideas?
Please check :
let input = [["H", "P", "C"], ["F", "K", "V"], ["J", "Y", "B"], ["A", "L"]]
var sortedArray = input.flatMap({ $0 }).sorted()
var finalArray:[[String]]=[]
var subArray:[String]=[]
for i in 0..<sortedArray.count {
subArray.append(sortedArray[i])
if subArray.count == 3 || i == sortedArray.count-1 {
finalArray.append(subArray)
subArray = []
}
}
print(finalArray)
// Output : [["A", "B", "C"], ["F", "H", "J"], ["K", "L", "P"], ["V", "Y"]]
func flattenArray(nestedArray: [[Character]]) -> [[Character]]{
var myFlattendArray = [Character]()
var sortedArray = [[Character]]()
for element in nestedArray{
if element is [Character]{
for char in element{
myFlattendArray.append(char)
}
}
}
myFlattendArray = myFlattendArray.sorted(by: {$0 < $1})
var arrayForArray = [Character]()
for i in 0..<myFlattendArray.count{
if((i % nestedArray.count == 0 && i != 0)){
sortedArray.append(arrayForArray)
arrayForArray.removeAll()
}else if i == myFlattendArray.count - 1{
arrayForArray.append(myFlattendArray[i])
sortedArray.append(arrayForArray)
arrayForArray.removeAll()
}
arrayForArray.append(myFlattendArray[i])
}
return sortedArray
}
You can use flatMap to flatten your array, sort it and then you can group using this extension from this answer as follow:
extension Array {
func group(of n: IndexDistance) -> Array<Array> {
return stride(from: 0, to: count, by: n)
.map { Array(self[$0..<Swift.min($0+n, count)]) }
}
}
let arr = [["H", "P", "C"],
["F", "K", "V"],
["J", "Y", "B"]]
let sorted = arr.flatMap{$0}.sorted().group(of: 3)
sorted // [["B", "C", "F"], ["H", "J", "K"], ["P", "V", "Y"]]

Beginner Swift: Converting string if letter in dictionary

Thanks for your help - just starting out so imagine there's some serious issues with my logic here.
Trying to write a program, given an input, if a letter from that input is found in a dictionary, replace the letter with the corresponding value.
Really stuck where I'm going wrong. Anyone able to help / suggest an alternative logic that may work?
var leetDic = ["A" : "4",
"B" : "6",
"E" : "3",
"I" : "1",
"L" : "1",
"M" : "(V)",
"N" : "(/)",
"O" : "0",
"S" : "5",
"T" : "7",
"V" : "(/",
"W" : "`//]"]
func leetConverter(leet: String) {
var leet1 = leet.uppercased()
for i in leet1.characters {
if i == leetDic.keys { //ERROR "Binary operator cannot be applied to operands of type Character and dictionary"
i = leetDic.values // ERROR "cannot assign value 'i' is a let constant"
} else {
return i
}
}
}
var test = leetConverter(leet: "Hello World")
Problem is you are comparing character and array also you can assign value to i in loop because it is constant you can go like this way.
func leetConverter(leet: String) -> String {
var leet1 = leet.uppercased()
var newString = String()
for ch in leet1.characters {
let str = String(ch)
if let value = leetDic[str] {
newString.append(value)
} else {
newString.append(str)
}
}
return newString
}
More Swifty way
func leetConverter(leet: String) -> String {
let str = leet.characters.map { leetDic[String($0).uppercased()] ?? String($0) }.joined()
return str
}

Swift 3: split a string to array by number [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have a string let string = "!101eggs". Now, I want to have an array like this ["!", "101", "e", "g", "g", "s"]. How can I do this?
I presume the hard part for you is "Where's the number"? As long as this is just a simple sequence of digits, a regular expression makes it easy to find:
let string = "!101eggs"
let patt = "\\d+"
let reg = try! NSRegularExpression(pattern:patt)
let r = reg.rangeOfFirstMatch(in: string,
options: [],
range: NSMakeRange(0,string.utf16.count)) // {1,3}
So now you know that the number starts at position 1 and is 3 characters long. The rest is left as an exercise for the reader.
Sorry It's too long
when input is
print("-1-2a000+4-1/000!00005gf101eg14g1s46nj3j4b1j5j23jj212j4b2j41234j01010101g0000z00005g0000".toArrayByNumber())
Result: ["-", "1", "-", "2", "a", "000", "+", "4", "-", "1", "/", "000", "!", "00005", "g", "f", "101", "e", "g", "14", "g", "1", "s", "46", "n", "j", "3", "j", "4", "b", "1", "j", "5", "j", "23", "j", "j", "212", "j", "4", "b", "2", "j", "41234", "j", "01010101", "g", "0000", "z", "00005", "g", "0000"]
extension Int {
func toZeroString() -> String {
return (0 ..< self).reduce("", { (result, zero) -> String in
return result + "0"
})
}
}
extension String {
func toArrayByNumber() -> [String] {
var array: [String] = []
var num = 0
var zeroCount = 0
var zeroEnd = false
for char in self.characters {
if let number = Int("\(char)") {
if zeroEnd == false && number == 0 {
zeroCount += 1
} else {
num = num * 10 + number
zeroEnd = true
}
} else {
if num != 0 {
array.append(zeroCount.toZeroString() + ("\(num)"))
} else if zeroCount > 0 {
array.append(zeroCount.toZeroString())
}
array.append(String(char))
num = 0
zeroCount = 0
zeroEnd = false
}
}
if num != 0 {
array.append(zeroCount.toZeroString() + ("\(num)"))
} else if zeroCount > 0 {
array.append(zeroCount.toZeroString())
}
return array
}
}