Browse Source

docs(firstword): require a function instead of a program

pull/2619/head
nprimo 4 months ago committed by Harry
parent
commit
252325d993
  1. 36
      subjects/firstword/README.md

36
subjects/firstword/README.md

@ -2,22 +2,44 @@
### Instructions
Write a program that takes an argument and displays its first word, followed by a newline (`'\n'`).
Write a function that takes a string and return a string containing its first word, followed by a newline (`'\n'`).
- A word is a sequence of characters delimited by spaces or by the start/end of the argument.
- The output will be followed by a newline (`'\n'`).
### Expected Function
- If the number of arguments is not 1, or if there are no words, the program displays nothing.
```go
func FirstWord(s string) string {
// ...
}
```
### Usage
Here is a possible way to test your function:
```go
package main
import (
"fmt"
"piscine"
)
func main() {
fmt.Print(piscine.FirstWord("hello there"))
fmt.Print(piscine.FirstWord(""))
fmt.Print(piscine.FirstWord("hello ......... bye"))
}
```
And its output:
```console
$ go run . "hello there"
$ go run .
hello
$ go run . "hello ......... bye"
hello
$ go run .
$ go run . "hello" "there"
$
```

Loading…
Cancel
Save