forked from root/public
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.2 KiB
60 lines
1.2 KiB
5 years ago
|
## atoi
|
||
5 years ago
|
|
||
5 years ago
|
### Instructions
|
||
5 years ago
|
|
||
5 years ago
|
- Write a function that simulates the behaviour of the `Atoi` function in Go. `Atoi` transforms a number represented as a `string` in a number represented as an `int`.
|
||
5 years ago
|
|
||
5 years ago
|
- `Atoi` returns `0` if the `string` is not considered as a valid number. For this exercise **non-valid `string` chains will be tested**. Some will contain non-digits characters.
|
||
5 years ago
|
|
||
5 years ago
|
- For this exercise the handling of the signs + or - **does have** to be taken into account.
|
||
5 years ago
|
|
||
5 years ago
|
- This function will **only** have to return the `int`. For this exercise the `error` result of atoi is not required.
|
||
5 years ago
|
|
||
![]()
5 years ago
|
### Expected function
|
||
5 years ago
|
|
||
|
```go
|
||
|
func Atoi(s string) int {
|
||
|
|
||
|
}
|
||
|
```
|
||
|
|
||
5 years ago
|
### Usage
|
||
5 years ago
|
|
||
5 years ago
|
Here is a possible program to test your function :
|
||
5 years ago
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
piscine ".."
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
![]()
4 years ago
|
fmt.Println(piscine.Atoi("12345"))
|
||
|
fmt.Println(piscine.Atoi("0000000012345"))
|
||
|
fmt.Println(piscine.Atoi("012 345"))
|
||
|
fmt.Println(piscine.Atoi("Hello World!"))
|
||
|
fmt.Println(piscine.Atoi("+1234"))
|
||
|
fmt.Println(piscine.Atoi("-1234"))
|
||
|
fmt.Println(piscine.Atoi("++1234"))
|
||
|
fmt.Println(piscine.Atoi("--1234"))
|
||
5 years ago
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$ go build
|
||
|
student@ubuntu:~/[[ROOT]]/test$ ./test
|
||
5 years ago
|
12345
|
||
|
12345
|
||
|
0
|
||
|
0
|
||
|
1234
|
||
|
-1234
|
||
|
0
|
||
|
0
|
||
5 years ago
|
student@ubuntu:~/[[ROOT]]/test$
|
||
5 years ago
|
```
|