|
|
|
@ -1,32 +1,29 @@
|
|
|
|
|
## Camel-to-snake-case |
|
|
|
|
## cameltosnakecase |
|
|
|
|
|
|
|
|
|
### Instructions |
|
|
|
|
|
|
|
|
|
Write a function that converts a string from `camelCase` to `snake_case`. |
|
|
|
|
Write a function that converts a `string` from `camelCase` to `snake_case`. |
|
|
|
|
|
|
|
|
|
For this exercise you need to know that camelCase has two different writing alternatives that will be accepted: |
|
|
|
|
- If the `string` is empty, return an empty `string`. |
|
|
|
|
- If the `string` is not `camelCase`, return the `string` unchanged. |
|
|
|
|
- If the `string` is `camelCase`, return the `snake_case` version of the `string`. |
|
|
|
|
|
|
|
|
|
For this exercise you need to know that `camelCase` has two different writing alternatives that will be accepted: |
|
|
|
|
|
|
|
|
|
- lowerCamelCase |
|
|
|
|
- UpperCamelCase |
|
|
|
|
|
|
|
|
|
Some rules for writing in camelCase: |
|
|
|
|
Rules for writing in `camelCase`: |
|
|
|
|
|
|
|
|
|
- The word does not end on a capitalized letter (CamelCasE). |
|
|
|
|
- No two capitalized letters shall follow directly each other (CamelCAse). |
|
|
|
|
- Numbers or punctuation are not allowed in the word anywhere (camelCase1). |
|
|
|
|
|
|
|
|
|
Here are some rules for you to follow: |
|
|
|
|
|
|
|
|
|
- If the string is empty, return an empty string. |
|
|
|
|
- If the string is not `camelCase`, return the string unchanged. |
|
|
|
|
- If the string is `camelCase`, return the `snake_case` version of the string. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Expected function |
|
|
|
|
|
|
|
|
|
```go |
|
|
|
|
func CamelToSnakeCase(s string) string{ |
|
|
|
|
//Your code here |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
@ -37,19 +34,23 @@ Here is a possible program to test your function:
|
|
|
|
|
```go |
|
|
|
|
package main |
|
|
|
|
|
|
|
|
|
import "fmt" |
|
|
|
|
import ( |
|
|
|
|
"fmt" |
|
|
|
|
|
|
|
|
|
"piscine" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
|
fmt.Println(CamelToSnakeCase("HelloWorld")) |
|
|
|
|
fmt.Println(CamelToSnakeCase("helloWorld")) |
|
|
|
|
fmt.Println(CamelToSnakeCase("camelCase")) |
|
|
|
|
fmt.Println(CamelToSnakeCase("CAMELtoSnackCASE")) |
|
|
|
|
fmt.Println(CamelToSnakeCase("camelToSnakeCase")) |
|
|
|
|
fmt.Println(CamelToSnakeCase("132322")) |
|
|
|
|
fmt.Println(piscine.CamelToSnakeCase("HelloWorld")) |
|
|
|
|
fmt.Println(piscine.CamelToSnakeCase("helloWorld")) |
|
|
|
|
fmt.Println(piscine.CamelToSnakeCase("camelCase")) |
|
|
|
|
fmt.Println(piscine.CamelToSnakeCase("CAMELtoSnackCASE")) |
|
|
|
|
fmt.Println(piscine.CamelToSnakeCase("camelToSnakeCase")) |
|
|
|
|
fmt.Println(piscine.CamelToSnakeCase("132322")) |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
and the output should be: |
|
|
|
|
And its output: |
|
|
|
|
|
|
|
|
|
```console |
|
|
|
|
$ go run . |
|
|
|
|