Skip to content

Easy challenges #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Aug 12, 2021
Prev Previous commit
Next Next commit
Camel Case
  • Loading branch information
ardallie committed Aug 11, 2021
commit 731b9122987f521477c89844d54c8060792c4639
65 changes: 65 additions & 0 deletions src/easy/CamelCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package easy;

/**
* Have the function CamelCase(str) take the str parameter being passed
* and return it in proper camel case format where the first letter
* of each word is capitalized (excluding the first letter).
* ---
* The string will only contain letters and some combination of delimiter
* punctuation characters separating each word.
* For example: if str is "BOB loves-coding" then your program
* should return the string bobLovesCoding.
*/
public class CamelCase {

/**
* A helper function which removes non-alphabetic characters,
* converts to a lower case and trims the string.
*
* @param str input string
* @return a string with non-alphabetic characters removed
*/
private static String[] splitWords(String str) {
return str
.toLowerCase()
.replaceAll("([^a-z])", " ")
.replaceAll(" +", " ")
.trim().split(" ");
}

/**
* Camel Case function.
*
* @param str input string
* @return a string in proper camel case format
*/
private static String camelCase(String str) {
StringBuilder camel = new StringBuilder();
String[] words = splitWords(str);
int index = 0;
for (String word : words) {
if (index == 0) {
camel.append(word);
} else {
camel.append(word.substring(0, 1).toUpperCase()).append(word.substring(1));
}
index++;
}
return camel.toString();
}

/**
* Entry point.
*
* @param args command line arguments
*/
public static void main(String[] args) {
var result1 = camelCase("_good_looking_blues_");
System.out.println(result1);

var result2 = camelCase("-=last-night-on-earth=-");
System.out.println(result2);

}

}
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