Counting Vowels in Swift [closed]

萝らか妹 提交于 2019-12-13 09:15:24

问题


I have done this successfully in Java and C++ but can't figure it out in Swift. Here is my C++ code:

int count_vowels (string input)
{
  int position = 0;

  for (int i = 0; i < input.length(); i++)
    {
    char ch;
    ch = input[i];

      if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
          position++;
      }

  }
  return position;
}

Again I'm just trying to iterate through a string, counting the values that are true in the if statement and returning the position(how many there are).

Is there any way to convert this to swift syntax.

I figured it out:

for char in vowels.characters{

            if char == ("a") || char == ("e") || char == ("i") || char == ("o") || char == ("u") {

                count++
            }
    }

Thanks everybody for there help and posts I was pulling my hair out on this one.

Thanks again


回答1:


You could do something like this:

extension String {
    var numberOfVowels: Int {
        let vowels = "aeiou"
        let vowelsSet = NSCharacterSet(charactersInString: vowels)
        let strippedComponents = lowercaseString.componentsSeparatedByCharactersInSet(vowelsSet)
        let stripped = strippedComponents.joinWithSeparator("")
        return characters.count - stripped.characters.count
    }
}

"Hello".numberOfVowels



回答2:


Disclaimer: I don't know Swift but....

You should be able to use Swift's higher level features to accomplish this more succinctly. For example, the vowel characters don't change within your function so they can be represented as a Swift Set:

var vowelCount = 0
var vowels = Set(["a", "e", "i", "o", "u"])

Then test if each input character is a member of the set and increment the count if it is:

for ch in input {
  if (vowels.contains(ch)) {
    vowelCount += 1
  }    
}



回答3:


I agree that you should show what it is you've tried in Swift so we can provide guidance on what may be broken. That being said, I did a quick conversion. It may not be the best, but here you go

import Cocoa

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

func count_vowels(input: String) -> Int {
    var vowelCount: Int = 0
    for ch in input.lowercaseString.characters {
        if (vowels.contains(ch)) {
            vowelCount++
        }
    }
    return vowelCount
}

With this, print(count_vowels("Hello world")) prints out 3

EDIT: Modified my answer based on the answer from Tom Hicks. It definitely gets rid of the ugly if statement and makes the code easier to read. I declare the vowels list outside of the function so that it isn't being generated every time you call the function.




回答4:


This will work ... but its not elegent.

let str = "test voWeL COUNT"

let v = ["A","a","E","e","i","I","o","O","u","U"]

var vowels = 0;

for i in str.characters {
    print(i)
    if (v.contains("\(i)")) {
        vowels++
    }
}

print("Vowels \(vowels)")


来源:https://stackoverflow.com/questions/32769582/counting-vowels-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!