Joseph Jude

Consult . Code . Coach

Swift Challenge 009: Count upper and lower case letters in a string


code . swift

Challenge: Write a program to count upper and lower case letters in a given string.

Test Cases:

Logic:

Code:

import Foundation

extension Character {
  func isUpper() -> Bool {
    if String(self).rangeOfString("[A-Z]", options: .RegularExpressionSearch) != nil {
      return true
    }
     return false
  }
}

extension Character {
  func isLower() -> Bool {
    if String(self).rangeOfString("[a-z]", options: .RegularExpressionSearch) != nil {
      return true
    }
     return false
  }
}

let inputString = "Hello @World #[email protected]"
var (upper,lower) = (0,0)

for c in inputString.characters {
  if c.isUpper() {
    upper += 1
  }

  if c.isLower() {
    lower += 1
  }
}

print(upper, lower)

Git Repository / All Swift Challenges

Got comments? Tweet it, or comment below.


Like the post? Retweet it. Got comments? Reply.

Count upper and lower case letters in a string (in #swift) by @jjude: https://t.co/8GybQgrlx9

— Joseph Jude (@jjude) November 8, 2016
Share this on: Twitter / /

Comments

comments powered by Disqus