Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

    }

    */
}
like image 200
Sachin Avatar asked Feb 22 '15 08:02

Sachin


People also ask

How do you check if a character in a string is uppercase?

To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase.

How do you check if a character is upper or lower in Java?

To check whether a character is in Uppercase or not in Java, use the Character. isUpperCase() method.

How do you know if a character is lower?

To check whether a character is in Lowercase or not in Java, use the Character. isLowerCase() method.


3 Answers

Swift 3

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

    if let scala = UnicodeScalar(string) {
      return set.contains(scala)
    } else {
      return false
    }
  }
like image 76
onmyway133 Avatar answered Sep 19 '22 03:09

onmyway133


 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
 }
}
like image 43
Ghanshyam Bhesaniya Avatar answered Sep 22 '22 03:09

Ghanshyam Bhesaniya


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)")
    }
}
like image 29
Andrew Tetlaw Avatar answered Sep 22 '22 03:09

Andrew Tetlaw