How many alphabets and numbers special characters in textfield [duplicate] - ios

I want to count the number of letters, digits and special characters in the following string:
let phrase = "The final score was 32-31!"
I tried:
for tempChar in phrase {
if (tempChar >= "a" && tempChar <= "z") {
letterCounter++
}
// etc.
but I'm getting errors. I tried all sorts of other variations on this - still getting error - such as:
could not find an overload for '<=' that accepts the supplied arguments

For Swift 5 see rustylepord's answer.
Update for Swift 3:
let letters = CharacterSet.letters
let digits = CharacterSet.decimalDigits
var letterCount = 0
var digitCount = 0
for uni in phrase.unicodeScalars {
if letters.contains(uni) {
letterCount += 1
} else if digits.contains(uni) {
digitCount += 1
}
}
(Previous answer for older Swift versions)
A possible Swift solution:
var letterCounter = 0
var digitCount = 0
let phrase = "The final score was 32-31!"
for tempChar in phrase.unicodeScalars {
if tempChar.isAlpha() {
letterCounter++
} else if tempChar.isDigit() {
digitCount++
}
}
Update: The above solution works only with characters in the ASCII character set,
i.e. it does not recognize Ä, é or ø as letters. The following alternative
solution uses NSCharacterSet from the Foundation framework, which can test characters
based on their Unicode character classes:
let letters = NSCharacterSet.letterCharacterSet()
let digits = NSCharacterSet.decimalDigitCharacterSet()
var letterCount = 0
var digitCount = 0
for uni in phrase.unicodeScalars {
if letters.longCharacterIsMember(uni.value) {
letterCount++
} else if digits.longCharacterIsMember(uni.value) {
digitCount++
}
}
Update 2: As of Xcode 6 beta 4, the first solution does not work anymore, because
the isAlpha() and related (ASCII-only) methods have been removed from Swift.
The second solution still works.

Use the values of unicodeScalars
let phrase = "The final score was 32-31!"
var letterCounter = 0, digitCounter = 0
for scalar in phrase.unicodeScalars {
let value = scalar.value
if (value >= 65 && value <= 90) || (value >= 97 && value <= 122) {++letterCounter}
if (value >= 48 && value <= 57) {++digitCounter}
}
println(letterCounter)
println(digitCounter)

For Swift 5 you can do the following for simple strings, but be vigilant about handling characters like "1️⃣" , "④" these would be treated as numbers as well.
let phrase = "The final score was 32-31!"
var numberOfDigits = 0;
var numberOfLetters = 0;
var numberOfSymbols = 0;
phrase.forEach {
if ($0.isNumber) {
numberOfDigits += 1;
}
else if ($0.isLetter) {
numberOfLetters += 1
}
else if ($0.isSymbol || $0.isPunctuation || $0.isCurrencySymbol || $0.isMathSymbol) {
numberOfSymbols += 1;
}
}
print(#"\#(numberOfDigits) || \#(numberOfLetters) || \#(numberOfSymbols)"#);

I've created a short extension for letter and digits count for a String
extension String {
var letterCount : Int {
return self.unicodeScalars.filter({ CharacterSet.letters.contains($0) }).count
}
var digitCount : Int {
return self.unicodeScalars.filter({ CharacterSet.decimalDigits.contains($0) }).count
}
}
or a function to get a count for any CharacterSet you put in
extension String {
func characterCount(for set: CharacterSet) -> Int {
return self.unicodeScalars.filter({ set.contains($0) }).count
}
}
usage:
let phrase = "the final score is 23-13!"
let letterCount = phrase.characterCount(for: .letters)

In case you only need one information (letter or number or sign) you can do it in one line:
let phrase = "The final score was 32-31!"
let count = phrase.filter{ $0.isLetter }.count
print(count) // "16\n"
But doing phrase.filter several times is inefficient because it loops through the whole string.

Related

How to create a regular expression to find out repeated character in string?

Eg:
HelloWorld - repeated characters are 5 (l is repeating 3 times and o is repeating 2 times)
Smart2000, repeated characters = 3?(0 is repeating 3 times)
Smart#200#12, repeated characters = 6
I tried with iterating over string
Here is my code with string iteration to find out the repeated character in string.
func countRepeatDigitsIn(keyword : String) -> Int
{
// To keep track of processed symbols
var uniqueCharacters = ""
var repeatCharacterCount = 0
for char in keyword.uppercased() {
let alphabet = String(char)
// If this is already counted, skip it
if (uniqueCharacters.contains(alphabet))
{
repeatCharacterCount += 1
}
// Otherwise, add it to processed symbols
uniqueCharacters += alphabet
}
return repeatCharacterCount
}
HelloWorld - repeated characters are 5 (l is repeating 3 times and o is repeating 2 times
The simplest way to get that result is to take a histogram and then add up all the values that are not 1.
Example:
func histogram(_ s:String) -> [Character:Int] {
var d = [Character:Int]()
for c in s {
d[c, default:0] += 1
}
return d
}
let reps = histogram("helloworld").values.filter{$0 > 1}.reduce(0, +) // 5
let reps2 = histogram("smart2000").values.filter{$0 > 1}.reduce(0, +) // 3
let reps3 = histogram("Smart#200#12").values.filter{$0 > 1}.reduce(0, +) // 6
Here's a fun chain of reductions and filters on the characters of the string.
func countRepeatDigitsIn(keyword : String) -> Int {
let total = Array(keyword.uppercased()).reduce(into: [Character : Int]()) { $0[$1, default: 0] += 1 }.filter { $0.value > 1 }.reduce(0) { $0 + $1.value }
return total
}
for text in ["HelloWorld", "Smart2000", "Smart#200#12"] {
print(text, countRepeatDigitsIn(keyword: text))
}
The first reduce builds a dictionary where the keys are characters and the values is the count for the character. Then the filter removes characters only found once. The second reduce adds the remaining counts.
Group same characters in a dictionary using Dictionary(grouping:by:) and add the counts of values where the count is greater than 1
extension String {
func repeatCount() -> Int {
return Dictionary(grouping: lowercased()) { $0 }.values.filter { $0.count > 1 }.reduce(0) { $0 + $1.count }
}
}
print("HelloWorLd".repeatCount())//5
print("Smart2000".repeatCount())//3
print("Smart#200#12".repeatCount())//6

How to replace a word in a string if it contains any words from an array [duplicate]

This question already has answers here:
Replace multiple words from a String based on the values in an Array
(4 answers)
Closed 3 years ago.
I'd like to find and replace any word(s) in a string that match any string value in an array. I can find the matching value using .contains(where:
var playersApprovedArray = ["Monica","Zach","Chrissy"]
card.cardText = "Chrissy, do 10 jumping jacks right now!"
if playersApprovedArray.contains(where: card.cardText.contains) {
print("Found matching player in card text")
// Replace the matching word/player here with another word/player
}
But I don't know how to replace the occurrence of the matching word with another string value in the array.
Here is a simple solution that replaces one player with another:
for i in playersApprovedArray.indices {
let player = playersApprovedArray[i]
if let range = cardText.range(of: player) {
let otherIndex = (i + Int.random(in: 1..<playersApprovedArray.count)) % playersApprovedArray.count
let otherPlayer = playersApprovedArray[otherIndex]
cardText.replaceSubrange(range, with: otherPlayer)
break
}
}
You can remove the break if the sentence may contains more players.
Here is a faster solution that would replace all occurrences of any player's name by another:
let playersApprovedArray = ["Monica","Zach","Chrissy"]
let cardText = " Chrissy, do 10 jumping jacks right now! "
var result = ""
var i = cardText.startIndex
while i < cardText.endIndex {
var j = i
while j < cardText.endIndex,
CharacterSet.letters.inverted.isSuperset(of: CharacterSet(charactersIn: String(cardText[j])))
{
j = cardText.index(after: j)
}
var tempo1 = ""
if i != j { tempo1 += cardText[i..<j] }
if j < cardText.endIndex { i = j } else {
result += tempo1
break
}
while j < cardText.endIndex,
CharacterSet.letters.isSuperset(of: CharacterSet(charactersIn: String(cardText[j])))
{
j = cardText.index(after: j)
}
let tempo2 = String(cardText[i..<j])
if let index = playersApprovedArray.firstIndex(of: tempo2) {
let otherIndex = (index + Int.random(in: 1..<playersApprovedArray.count)) % playersApprovedArray.count
let otherPlayer = playersApprovedArray[otherIndex]
result += tempo1 + otherPlayer
//See comment below for possible early exit
} else {
result += tempo1 + tempo2
}
i = j
}
print(result) // Monica, do 10 jumping jacks right now!
If you're sure that there is only one player in the string, then you can exit early this way:
...
result += tempo1 + otherPlayer + cardText[j...]
break
...

Making a binary calculator app (swift)

For a project in Uni I decided to make a binary calculator app to learn a little bit more about coding.
I've come as far as completing a regular calculator app (but it only has two numbers: 1 ; 0) but I can't figure out how to make the calculator work like it should ( 1010 + 1101 = 10111 not 2111). All help is appreciated.
var numberEkraanil:Double = 0;
var eelmineNumber:Double = 0;
var tehesmatemaatikat = false
var operation = 0;
#IBOutlet weak var label: UILabel!
#IBAction func Numbers(_ sender: UIButton) {
if tehesmatemaatikat == true
{
label.text = String(sender.tag-1)
numberEkraanil = Double(label.text!)!
tehesmatemaatikat = false
}
else
{
label.text = label.text! + String(sender.tag-1)
numberEkraanil = Double(label.text!)!
}
}
#IBAction func nupud(_ sender: UIButton) {
if label.text != "" && sender.tag != 6 && sender.tag != 8
{
eelmineNumber = Double(label.text!)!
if sender.tag == 3 //Liitmine
{
label.text = "+";
}
else if sender.tag == 4 //Lahutamine
{
label.text = "-";
}
else if sender.tag == 5 // Korrutamine
{
label.text = "x";
}
else if sender.tag == 7 // Jagamine
{
label.text = "÷";
}
operation = sender.tag
tehesmatemaatikat = true;
}
else if sender.tag == 8
{
if operation == 3
{
label.text = String(eelmineNumber + numberEkraanil)
}
else if operation == 4
{
label.text = String(eelmineNumber - numberEkraanil)
}
else if operation == 5
{
label.text = String(eelmineNumber * numberEkraanil)
}
else if operation == 7
{
label.text = String(eelmineNumber / numberEkraanil)
}
}
else if sender.tag == 6
{
label.text = ""
eelmineNumber = 0;
numberEkraanil = 0;
operation = 0;
}
You can possibly convert binary numbers to decimal numbers. For example turn "1010" into int "10" and then reverse the process to get the binary again. In your example "1010 + 1101 = 10111" you can convert "1010" and into "10" and "13", make the ordinary calculation with those decimals and convert the result "23", which will give you "23".
But of course there are other ways. This website can help you with binary calculation. It's a math website: http://www.calculator.net/binary-calculator.html.
You will need to write a base 10 to base 2 and a base 2 to base 10 converter. Here is pseudo-code for both:
To convert a binary string to an integer, do the following:
Seed a result value to zero.
while your input string is not empty:
shift the result value 1 bit to the left
remove the left-most character from your input string, and if it's a 1, add 1 to your result value.
To convert an int to a binary string, do the same in reverse:
Copy your int to a scratch variable, scratch.
Set your output string to an empty string.
While scratch is not 0:
if scratch && 1 is 0, append a "0" to the left of your output string
if scratch && 1 is 1, append a "1" to the left of your output string
shift scratch 1 bit to the right
Once you have those building-blocks getting your calculator to work is pretty straightforward. When the user inputs a binary string, convert it to an integer working value. Do your calculations on integer values, and then convert the integer result to a binary string for display.

Random number from an array without repeating the same number twice in a row?

I am making a game using Swift and SpriteKit where i move an object to random locations based on an array.
The array that is made up of CGPoints:
let easyArray = [CGPointMake(0,0), CGPointMake(126.6,0), CGPointMake(253.4,0), CGPointMake(0,197.5), CGPointMake(126.7,197.5), CGPointMake(253.4,197.5), CGPointMake(0,395), CGPointMake(126.7,395), CGPointMake(253.4,395)]
I use this function to generate a random number:
func randomNumber(maximum: UInt32) -> Int {
var randomNumber = arc4random_uniform(maximum)
while previousNumber == randomNumber {
randomNumber = arc4random_uniform(maximum)
}
previousNumber = randomNumber
return Int(randomNumber)
}
I used this to move the object based on the random number generated:
let greenEasy = randomNumberNew(9)
let moveSelector = SKAction.moveTo(easyArray[greenEasy], duration: 0)
selector.runAction(moveSelector)
I have done some reading online and found that the "While" condition should make it so that the same random number isn't generate twice in a row. But it still happens.
Can anyone please help me on how to make it so i don't get the same number twice in a row?
The code below doesn't random the same number.
var currentNo: UInt32 = 0
func randomNumber(maximum: UInt32) -> Int {
var randomNumber: UInt32
do {
randomNumber = (arc4random_uniform(maximum))
}while currentNo == randomNumber
currentNo = randomNumber
return Int(randomNumber)
}
I think Larme's suggestion is pretty clever, actually.
easyArray.append(easyArray.removeAtIndex(Int(arc4random_uniform(UInt32(easyArray.count)-1))))
selector.runAction(SKAction.moveTo(easyArray.last!, duration: 0))
I would recommend to not use while() loops with randomizers.
Theoretically it can cause infinite loops in worst case scenario, in more positive scenario it will just take few loops before you get desired results.
Instead I would advice to make an NSArray of all values, remove from this NSArray last randomized element and randomize any of other existing elements from such an array - that is guarantee result after only one randomize iteration.
It can be easily achieved by making NSArray category in Objective-C:
- (id) randomARC4Element
{
if(self.count > 0)
{
return [self objectAtIndex:[self randomIntBetweenMin:0 andMax:self.count-1]];
}
return nil;
}
- (int)randomIntBetweenMin:(int)minValue andMax:(int)maxValue
{
return (int)(minValue + [self randomFloat] * (maxValue - minValue));
}
- (float)randomFloat
{
return (float) arc4random() / UINT_MAX;
}
If you can use linq then you can select a random value that doesn't match the last value. Then for any left over values you can loop through and find valid places to insert them.
It's not the most efficient but it works.
public static List<int> Randomize(List<int> reps, int lastId) {
var rand = new Random();
var newReps = new List<int>();
var tempReps = new List<int>();
tempReps.AddRange(reps);
while (tempReps.Any(x => x != lastId)) {
var validReps = tempReps.FindAll(x => x != lastId);
var i = rand.Next(0, validReps.Count - 1);
newReps.Add(validReps[i]);
lastId = validReps[i];
tempReps.Remove(validReps[i]);
}
while (tempReps.Any()) {
var tempRep = tempReps.First();
bool placed = false;
for (int i = 0; i < newReps.Count; i++) {
if (newReps[i] == tempRep) {
continue;
}
else if ((i < newReps.Count - 1) && (newReps[i + 1] == tempRep)) {
continue;
}
else {
newReps.Insert(i + 1, tempRep);
placed = true;
break;
}
}
if (placed) {
tempReps.Remove(tempRep);
}
else {
throw new Exception("Unable to randomize reps");
}
}
return newReps;
}

How to find out if letter is Alphanumeric or Digit in Swift

I want to count the number of letters, digits and special characters in the following string:
let phrase = "The final score was 32-31!"
I tried:
for tempChar in phrase {
if (tempChar >= "a" && tempChar <= "z") {
letterCounter++
}
// etc.
but I'm getting errors. I tried all sorts of other variations on this - still getting error - such as:
could not find an overload for '<=' that accepts the supplied arguments
For Swift 5 see rustylepord's answer.
Update for Swift 3:
let letters = CharacterSet.letters
let digits = CharacterSet.decimalDigits
var letterCount = 0
var digitCount = 0
for uni in phrase.unicodeScalars {
if letters.contains(uni) {
letterCount += 1
} else if digits.contains(uni) {
digitCount += 1
}
}
(Previous answer for older Swift versions)
A possible Swift solution:
var letterCounter = 0
var digitCount = 0
let phrase = "The final score was 32-31!"
for tempChar in phrase.unicodeScalars {
if tempChar.isAlpha() {
letterCounter++
} else if tempChar.isDigit() {
digitCount++
}
}
Update: The above solution works only with characters in the ASCII character set,
i.e. it does not recognize Ä, é or ø as letters. The following alternative
solution uses NSCharacterSet from the Foundation framework, which can test characters
based on their Unicode character classes:
let letters = NSCharacterSet.letterCharacterSet()
let digits = NSCharacterSet.decimalDigitCharacterSet()
var letterCount = 0
var digitCount = 0
for uni in phrase.unicodeScalars {
if letters.longCharacterIsMember(uni.value) {
letterCount++
} else if digits.longCharacterIsMember(uni.value) {
digitCount++
}
}
Update 2: As of Xcode 6 beta 4, the first solution does not work anymore, because
the isAlpha() and related (ASCII-only) methods have been removed from Swift.
The second solution still works.
Use the values of unicodeScalars
let phrase = "The final score was 32-31!"
var letterCounter = 0, digitCounter = 0
for scalar in phrase.unicodeScalars {
let value = scalar.value
if (value >= 65 && value <= 90) || (value >= 97 && value <= 122) {++letterCounter}
if (value >= 48 && value <= 57) {++digitCounter}
}
println(letterCounter)
println(digitCounter)
For Swift 5 you can do the following for simple strings, but be vigilant about handling characters like "1️⃣" , "④" these would be treated as numbers as well.
let phrase = "The final score was 32-31!"
var numberOfDigits = 0;
var numberOfLetters = 0;
var numberOfSymbols = 0;
phrase.forEach {
if ($0.isNumber) {
numberOfDigits += 1;
}
else if ($0.isLetter) {
numberOfLetters += 1
}
else if ($0.isSymbol || $0.isPunctuation || $0.isCurrencySymbol || $0.isMathSymbol) {
numberOfSymbols += 1;
}
}
print(#"\#(numberOfDigits) || \#(numberOfLetters) || \#(numberOfSymbols)"#);
I've created a short extension for letter and digits count for a String
extension String {
var letterCount : Int {
return self.unicodeScalars.filter({ CharacterSet.letters.contains($0) }).count
}
var digitCount : Int {
return self.unicodeScalars.filter({ CharacterSet.decimalDigits.contains($0) }).count
}
}
or a function to get a count for any CharacterSet you put in
extension String {
func characterCount(for set: CharacterSet) -> Int {
return self.unicodeScalars.filter({ set.contains($0) }).count
}
}
usage:
let phrase = "the final score is 23-13!"
let letterCount = phrase.characterCount(for: .letters)
In case you only need one information (letter or number or sign) you can do it in one line:
let phrase = "The final score was 32-31!"
let count = phrase.filter{ $0.isLetter }.count
print(count) // "16\n"
But doing phrase.filter several times is inefficient because it loops through the whole string.

Resources