Preface
I have not had the chance to practice after learning Swift recently. I found that due to the historical development of Swift, most of the algorithms on the Internet are implemented in C, Java or other languages, and almost no Swift is implemented. Therefore, I plan to use Swift to implement some mainstream algorithms, which is not only a review of my own Swift, but also an improvement in my own algorithms.
First of all, use Swift to implement string to numbers. Of course, you must not use Swift’s own string to numbers API.
topic:
Implement a method using Swift, input a string, and output the number converted to the string.
For example, enter the string "125" and output the number 125
Implementation ideas and code
First consider possible input situations, including illegal input:
Case 1: All characters can be converted directly into numbers, such as "125"
Case 2: Contains one or more positive signs, such as "-125", "-+125"
Case 3: Contains illegal characters such as "125lff"
If it is case 1, it will be very simple to process: first use ASCII encoding to convert each character of the string "125" into a number, then convert it into a number and then use multiplication and addition directly: 1*100+2*10+3=123.
But the actual situation is not that simple. Due to the existence of situation 2 and 3, it is definitely not feasible to use multiplication and addition directly above, and corresponding judgments must be added in the middle. The best way is to directly traverse the characters in the string, first assume that there are no positive and negative signs and illegal characters. When traversing to the first character "1", intStr=1, when traversing to the second character "2", intStr = intStr*10+2=12, and when traversing to the last character "3", intStr = intStr*10+3=123.
Regarding "+" and "-", they represent positive and negative only when they start with the string. Existing in the middle of the string is an illegal character. We can judge "+" and "-" through ASCII encoding (the corresponding values are 43 and 45 respectively), and let a variable s represent the positive and negative after accumulation of multiple "+" and "-". The final result is intStr = s * intStr. When illegal characters are found during the traversal of the string, the subsequent traversal is terminated and a prompt is given.
Implementation code:
//Stand to numberfunc StrToInt(str:String) -> Int{ //The string cannot be empty guard == false else { print("The string cannot be empty~"); return 0; } var s = 1 var strInt:Int? = nil for characterInt in { //Only include numbers or positive and negative signs let tempStrInt = - "0".!.hashValue guard (tempStrInt <= 9 && tempStrInt >= 0) || ( == 43 || == 45) else { print("Contains illegal characters!"); return 0; } //The positive and negative signs can only exist at the beginning of the string if == 43 || == 45 { guard strInt == nil else { print("The positive and negative signs can only exist at the beginning of the string!"); return 0; } } //Since you have reached this point, it means that the string is legal //Judge positive and negative numbers if == 43 || == 45{ s = s * ( 44 - ) }else{ if strInt == nil { strInt = - "0".!.hashValue }else{ //Use overflow operators &* and &+ to avoid overflow crash caused by excessive value strInt = strInt! &* 10 &+ ( - "0".!.hashValue ) } } } var result:Int? = 0 if strInt != nil { result = s * strInt! } return result!; }
In the algorithm implemented above:
Input "125" and output 125
Input "+-125" and output -125
Enter "1-25", and prompt that "the positive and negative signs can only exist at the beginning of the string!"
Enter "1m25" and prompt "contains illegal characters"
Summarize
The above is all about the Swift algorithm implementing string conversion to numbers. I hope that the content of this article will be of some help to everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.