6

I am trying to make a program that stores a string in a variable called input.

With this input variable, I am then trying to convert it to an array, and then test with a for loop whether each character in the array is lowerCase or not. How can I achieve this?

Here is how far I have gotten:

var input = "The quick BroWn fOX jumpS Over tHe lazY DOg"

var inputArray = Array(input)

for character in inputArray {
    /*

    if character is lower case {

        make it uppercase

    } else {

        make it lowercase

    }

    */
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sachin
  • 737
  • 1
  • 9
  • 23
  • I have closed as a duplicate because your actual intention seems to be to *swap cases* (and in any case, you could use the methods from the answers to that question). – Please let me know if I was completely wrong, then I will reopen the question. – Martin R Feb 22 '15 at 08:30
  • This isnt a duplicate. I have written above that I checked other posts and was unable to find a question. I dont want an answer which uses a function. Please reopen the question. @MartinR – Sachin Feb 22 '15 at 08:48
  • 4
    Perhaps you can explain why the various answers to [Swap string case - swift](http://stackoverflow.com/questions/28255709/swap-string-case-swift) do not work for you. You don't have to use a function, you can just use use the code inside those functions. – I still think this is a duplicate, but I have reopened the question to that anybody can jump in. – Martin R Feb 22 '15 at 09:14

8 Answers8

6

Swift 3

static func isLowercase(string: String) -> Bool {
    let set = CharacterSet.lowercaseLetters

    if let scala = UnicodeScalar(string) {
      return set.contains(scala)
    } else {
      return false
    }
  }
onmyway133
  • 45,645
  • 31
  • 257
  • 263
4
 var input = "The quick BroWn fOX jumpS Over tHe lazY DOg"

 var inputArray = Array(input)

 for character in inputArray {

 var strLower = "[a-z]";

 var strChar = NSString(format: "%c",character )
 let strTest = NSPredicate(format:"SELF MATCHES %@", strLower );
 if strTest .evaluateWithObject(strChar)
 {
   // lower character
 }
 else
 {
   // upper character
 }
}
4

Swift 4:

var input = "The quick BroWn fOX jumpS Over tHe lazY DOg"
let uppers = CharacterSet.uppercaseLetters
let lowers = CharacterSet.lowercaseLetters
input.unicodeScalars.forEach {
    if uppers.contains($0) {
        print("upper: \($0)")
    } else if lowers.contains($0) {
        print("lower: \($0)")
    }
}
Andrew Tetlaw
  • 2,669
  • 22
  • 27
2

If I had to, I'd probably code something like this:

extension Character {
    func isMember(of set: CharacterSet) -> Bool {
        guard let scalar = UnicodeScalar(String(self)) else { return false }
        return set.contains(scalar)
    }
    var isUppercase: Bool {
        return isMember(of: .uppercaseLetters)
    }
    // var isLowercase, isWhitespace, etc.
}

let result = "The quick BroWn fOX jumpS Over tHe lazY DOg"
    .map { $0.isUppercase ? String($0).lowercased() : String($0).uppercased() }
    .joined()
print(result)

// "tHE QUICK bROwN Fox JUMPs oVER ThE LAZy doG"
markiv
  • 1,578
  • 16
  • 12
0

You should use regexp: grep [A-Z] versus grep [a-z].

0

Here's an answer written on Swift 4 that works wether the input String is a single or multiple letters:

  extension String {
      static func isLowercase(string: String) -> Bool {
          let set = CharacterSet.lowercaseLetters
          for character in string {
              if let scala = UnicodeScalar(String(character)) {
                  if !set.contains(scala) {
                      return false
                  }
              }
          }
          return true
      }

      static func isUppercase(string: String) -> Bool {
          let set = CharacterSet.uppercaseLetters
          for character in string {
              if let scala = UnicodeScalar(String(character)) {
                  if !set.contains(scala) {
                      return false
                  }
              }
          }
          return true
      }
  }
0

A solution based on character comparison

let input = "The quick BroWn fOX jumpS Over tHe lazY DOg"

for char in input {
    if char >= "A" && char <= "Z" {
        print("upper: \(char)")
    } else if char >= "a" && char <= "z" {
        print("lower: \(char)")
    } else {
        print("something else: '\(char)'")
    }
}
ArunGJ
  • 2,685
  • 21
  • 27
-3

You can check the ascii value of each characters because upper case and lower case are different value since upper are 65-90 and lower are 97-122

Joe
  • 337
  • 2
  • 16