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
}
*/
}
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.
To check whether a character is in Uppercase or not in Java, use the Character. isUpperCase() method.
To check whether a character is in Lowercase or not in Java, use the Character. isLowerCase() method.
Swift 3
static func isLowercase(string: String) -> Bool {
let set = CharacterSet.lowercaseLetters
if let scala = UnicodeScalar(string) {
return set.contains(scala)
} else {
return false
}
}
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
}
}
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)")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With