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.
miguel
7b50c69f22
|
9 months ago | |
---|---|---|
.. | ||
README.md | 1 year ago | |
main.go | 9 months ago |
README.md
cameltosnakecase
Instructions
Write a function that converts a string
from camelCase
to snake_case
.
- If the
string
is empty, return an emptystring
. - If the
string
is notcamelCase
, return thestring
unchanged. - If the
string
iscamelCase
, return thesnake_case
version of thestring
.
For this exercise you need to know that camelCase
has two different writing alternatives that will be accepted:
- lowerCamelCase
- UpperCamelCase
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).
Expected function
func CamelToSnakeCase(s string) string{
}
Usage
Here is a possible program to test your function:
package main
import (
"fmt"
)
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("hey2"))
}
And its output:
$ go run .
Hello_World
hello_World
camel_Case
CAMELtoSnackCASE
camel_To_Snake_Case
hey2