\"Camel Case\" is a way of writing a phrase without the use of spaces, but which
ID: 3718151 • Letter: #
Question
"Camel Case" is a way of writing a phrase without the use of spaces, but which allows its individual words to be read separately. Most of the phrase is rendered in lowercase, but the first letter of each word is rendered in uppercase. For example, the phrase "user account info" would be rendered in camel case as "UserAccountInfo".
Write a method called toCamelCase that takes as an argument phrase and returns that phrase may contain any number of words. You may assume that the phrase contains no digits or punctuation, and that words are separated by single spaces (no bad input will be given).
Explanation / Answer
public class Driver {
public static String toCamelCase(String phrase) {
String newWord = "";
newWord = String.valueOf(Character.toUpperCase(phrase.charAt(0)));
for(int i = 1; i < phrase.length(); i++) {
if(phrase.charAt(i - 1) == ' ')
newWord += String.valueOf(Character.toUpperCase(phrase.charAt(i)));
else if(phrase.charAt(i) != ' ')
newWord += String.valueOf(phrase.charAt(i));
}
return newWord;
}
public static void main(String[] args){
System.out.println(toCamelCase("user account info"));
}
}
**Comment for any further queries.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.