Have the function LetterCapitalize(str) take the str parameter being passed and capitalize the first letter of each word. Words will be separated by only one space.
Letter Capitalize Coderbyte Java
import java.util.Scanner;
public class LetterCapitalize {
String LetterCapitalize(String str) {
String words[] = str.split("\\s");
StringBuilder result = new StringBuilder(str);
int place = 0;
for (int i = 0; i < words.length; i++) {
if (place == 0) {
result.setCharAt(0, Character.toUpperCase(words[i].charAt(0)));
} else {
result.setCharAt(place, Character.toUpperCase(words[i].charAt(0)));
}
place += words[i].length() + 1;
}
return result.toString();
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
LetterCapitalize c = new LetterCapitalize();
System.out.print(c.LetterCapitalize(s.nextLine()));
}
}
Letter Capitalize Coderbyte Python
def LetterCapitalize(str):
return " ".join(word.capitalize() for word in str.split())
# keep this function call here
# to see how to enter arguments in Python scroll down
print LetterCapitalize(raw_input())
Letter Capitalize Coderbyte JavaScript
Using the JavaScript language, have the function LetterCapitalize(str) take the str parameter being passed and capitalize the first letter of each word. Words will be separated by only one space.
function LetterCapitalize(str) {
var arr = str.split(" ");
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1);
}
return arr.join(" ");
}
Explanation:
I am going to convert the string to an array based on the space character that separates each word. Then I am going to loop through each word in the array and capitalize the first letter of the word. I am going to convert array back to a string to return as the answer.
Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.