DEV Community

Discussion on: Learning Algorithms with JS, Python and Java 8: Sentence Capitalization

Collapse
 
tommy3 profile image
tommy-3

Thank you. This is nice!

A Java equivalent would be:

import java.util.stream.Collectors;
import java.util.stream.Stream;

public static String capitalize(String sentence) {
    return Stream.of(sentence.split(" "))
            .map(word -> String.valueOf(word.charAt(0)).toUpperCase() + word.substring(1))
            .collect(Collectors.joining(" "));
}
Enter fullscreen mode Exit fullscreen mode

I tried it in Python, too:

def capitalize(sentence: str) -> str:
    return ' '.join(map(lambda word: word[0].upper() + word[1:], sentence.split()))
Enter fullscreen mode Exit fullscreen mode

though I find this far less understandable than my original solutions using list comprehensions.