mirror of https://github.com/01-edu/public.git
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.1 KiB
60 lines
1.1 KiB
2 years ago
|
## atoi
|
||
|
|
||
|
### Instructions
|
||
|
|
||
|
- 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`.
|
||
|
|
||
|
- `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.
|
||
|
|
||
|
- For this exercise the handling of the signs `+` or `-` **does have** to be taken into account.
|
||
|
|
||
|
- This function will **only** have to return the `int`. For this exercise the `error` result of `Atoi` is not required.
|
||
|
|
||
|
### Expected function
|
||
|
|
||
|
```go
|
||
|
func Atoi(s string) int {
|
||
|
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Usage
|
||
|
|
||
|
Here is a possible program to test your function :
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
func main() {
|
||
|
fmt.Println(Atoi("12345"))
|
||
|
fmt.Println(Atoi("0000000012345"))
|
||
|
fmt.Println(Atoi("012 345"))
|
||
|
fmt.Println(Atoi("Hello World!"))
|
||
|
fmt.Println(Atoi("+1234"))
|
||
|
fmt.Println(Atoi("-1234"))
|
||
|
fmt.Println(Atoi("++1234"))
|
||
|
fmt.Println(Atoi("--1234"))
|
||
|
}
|
||
|
```
|
||
|
|
||
|
And its output :
|
||
|
|
||
|
```console
|
||
|
$ go run .
|
||
|
12345
|
||
|
12345
|
||
|
0
|
||
|
0
|
||
|
1234
|
||
|
-1234
|
||
|
0
|
||
|
0
|
||
|
$
|
||
|
```
|
||
|
|
||
|
### Notions
|
||
|
|
||
|
- [strconv/Atoi](https://golang.org/pkg/strconv/#Atoi)
|