forked from root/public
Compare commits
6 Commits
master
...
1156-camel
Author | SHA1 | Date |
---|---|---|
Hamza elkhatri | 24d61abd6a | 2 years ago |
Hamza elkhatri | 2999046b6c | 2 years ago |
Hamza elkhatri | c0d6845e90 | 2 years ago |
Hamza elkhatri | c5c95d9a00 | 2 years ago |
Hamza elkhatri | aec57d5710 | 2 years ago |
hamza | ee5349a851 | 2 years ago |
1 changed files with 54 additions and 0 deletions
@ -0,0 +1,54 @@
|
||||
## Camel-to-snake-case |
||||
|
||||
### Instructions |
||||
Write a function that converts a string from `CamelCase` to `snake_case`. |
||||
|
||||
Camel case is the practice of writing phrases without spaces or punctuation, it indicates the separation of two words with a single capitalized letter. Snake case is a style of writing in which each space is replaced by an underscore (_) character. |
||||
|
||||
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. |
||||
|
||||
Basic `CamelCase` Capitalization Rules: |
||||
|
||||
- The first letter must be capitalized. |
||||
- The word must not have two capitalized letters together (CamelCAse) nor end with a capitalized letter (CamelCasE). |
||||
- No numbers or punctuations are allowed in the word at any place (CamelCase1more). |
||||
|
||||
|
||||
### Expected function |
||||
```go |
||||
func CamelToSnakeCase(s string) string{ |
||||
//Your code here |
||||
} |
||||
``` |
||||
|
||||
### Usage |
||||
|
||||
Here is a possible program to test your function: |
||||
|
||||
```go |
||||
package main |
||||
|
||||
import "fmt" |
||||
|
||||
func main() { |
||||
fmt.Println(CamelToSnakeCase("HelloWorld")) |
||||
fmt.Println(CamelToSnakeCase("helloWorld")) |
||||
fmt.Println(CamelToSnakeCase("CamelToSnakeCase")) |
||||
fmt.Println(CamelToSnakeCase("132322")) |
||||
} |
||||
``` |
||||
|
||||
and the output should be: |
||||
|
||||
```console |
||||
$ go run . |
||||
Hello_World |
||||
helloWorld |
||||
Camel_To_Snake_Case |
||||
132322 |
||||
``` |
||||
|
Loading…
Reference in new issue