Skip to content

Commit 85cf1f7

Browse files
committed
Snake Case
1 parent 9b6d070 commit 85cf1f7

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/easy/SnakeCase.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package easy;
2+
3+
/**
4+
* Have the function SnakeCase(str) take the str parameter being passed
5+
* and return it in proper snake case format where each word is lowercased
6+
* and separated from adjacent words via an underscore.
7+
* The string will only contain letters and some combination
8+
* of delimiter punctuation characters separating each word.
9+
*/
10+
public class SnakeCase {
11+
12+
/**
13+
* Snake Case function.
14+
*
15+
* @param str input string
16+
* @return a string in a snake case format
17+
*/
18+
private static String snakeCase(String str) {
19+
return str
20+
.toLowerCase()
21+
.replaceAll("([^a-z])", " ")
22+
.replaceAll(" +", "_")
23+
.trim();
24+
}
25+
26+
/**
27+
* Entry point.
28+
*
29+
* @param args command line arguments
30+
*/
31+
public static void main(String[] args) {
32+
var result1 = snakeCase("Revolt is the right of the people");
33+
System.out.println(result1);
34+
var result2 = snakeCase("Fortitude is the guard and support of the other virtues");
35+
System.out.println(result2);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy