Sachin
Sachin

Reputation: 765

Check if a character is lowerCase or upperCase

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

    }

    */
}

Upvotes: 6

Views: 9599

Answers (8)

Joe
Joe

Reputation: 357

You can compare the ASCII value of each character because upper case and lower case have a different value:

  • Numbers is 48...57
  • Uppercase is 65...90
  • Lowercase is 97...122

The ASCII value can be obtained through:

input.utf8

Read more: https://developer.apple.com/documentation/swift/string/utf8view

Upvotes: -2

ArunGJ
ArunGJ

Reputation: 2683

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)'")
    }
}

Upvotes: 0

markiv
markiv

Reputation: 1664

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"

Upvotes: 2

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
      }
  }

Upvotes: 0

Andrew Tetlaw
Andrew Tetlaw

Reputation: 2689

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)")
    }
}

Upvotes: 4

onmyway133
onmyway133

Reputation: 48055

Swift 3

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

    if let scala = UnicodeScalar(string) {
      return set.contains(scala)
    } else {
      return false
    }
  }

Upvotes: 6

Ghanshyam Bhesaniya
Ghanshyam Bhesaniya

Reputation: 92

 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
 }
}

Upvotes: 4

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

Upvotes: 0

Related Questions